{"id":4846,"date":"2012-10-10T07:00:00","date_gmt":"2012-10-10T07:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/10\/use-powershell-3-0-to-filter-files-based-on-advanced-attributes\/"},"modified":"2012-10-10T07:00:00","modified_gmt":"2012-10-10T07:00:00","slug":"use-powershell-3-0-to-filter-files-based-on-advanced-attributes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-3-0-to-filter-files-based-on-advanced-attributes\/","title":{"rendered":"Use PowerShell 3.0 to Filter Files Based on Advanced Attributes"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell 3.0 for file attribute filtering.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I will be at the Copenhagen Windows PowerShell User Group in a about a month. Teresa has been corresponding with Windows PowerShell MVP, Claus T. Nielsen, about the agenda for the meeting. It is official, I will be talking about the new features in Windows PowerShell 3.0. The initial details are available on the <a href=\"http:\/\/www.psug.dk\/\" target=\"_blank\">Copenhagen Windows PowerShell User Group<\/a> website. It will be awesome because Teresa and I have not been in Copenhagen for more than five years. We are really looking forward to going back.<\/p>\n<h2>Using Get-ChildItem built-in aliases<\/h2>\n<p>In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/08\/finding-readonly-and-system-files-using-powershell-3-0.aspx\" target=\"_blank\">Finding Read-Only and System Files Using PowerShell 3.0<\/a>, I talked about using the new switches to find hidden, system, or read-only files by using the <b>Get-ChildItem<\/b> cmdlet.<\/p>\n<p>One of the things I did not talk about is that each of these new switches also have a built-in alias. Therefore, when looking for read-only files, I can use the command shown here.<\/p>\n<p style=\"padding-left: 30px\">dir -File -ReadOnly &ndash;Force<\/p>\n<p>But if I want to, I can shorten the command by using an alias, <b>ar<\/b><i>, <\/i>in place of <b>readonly<\/b><i>. <\/i>This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir -File -ar &ndash;Force<\/p>\n<p>On my computer, the previous command saves one key stroke. This is because I use tab completion, and <b>recurse<\/b><i> <\/i>appears when I press <b>r<\/b>&lt;tab&gt; the first time. When I press the second &lt;tab&gt; I get the <b>Recurse<\/b><i> <\/i>parameter. Therefore, I type the following:<\/p>\n<p style=\"padding-left: 30px\">R&lt;tab&gt;&lt;tab&gt;<\/p>\n<p>This equals three key strokes. On the other hand, by using the alias, I only type the following:<\/p>\n<p style=\"padding-left: 30px\">Ar<\/p>\n<p>This is two key strokes. It is not quite such a big deal. In addition, the alias <b>ar<\/b><i> <\/i>is not very readable. But when working from the command line, aliases are very useful to reduce typing, in addition to reducing the amount of space that is used by the command.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;T<\/b>he <b>Force<\/b><i> <\/i>parameter is not required when using the <b>ReadOnly<\/b><i> <\/i>parameter. Because in the root of my laptop running Windows 8, there is only one read-only file, and it is hidden and a system file, I have to use the <b>Force<\/b><i> <\/i>parameter to see the file.<\/p>\n<p>The alias for the <b>Hidden<\/b><i> <\/i>switch, it is <b>ah<\/b><i>. <\/i>The following command retrieves hidden files.<\/p>\n<p style=\"padding-left: 30px\">dir -File -ah<\/p>\n<p>The alias for the <b>System<\/b><i> <\/i>switch is <b><i>as<\/i><\/b><i>. <\/i>The following command retrieves system files.<\/p>\n<p style=\"padding-left: 30px\">dir -File -Force &ndash;as<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp;&nbsp;&nbsp;The pattern is <b>a<\/b><i> <\/i>for attribute, and then the first letter of the attribute name, for example: r, h, s.<\/p>\n<h2>Using attribute filters<\/h2>\n<p>In addition to having several new switches that simplify finding files with a particular attribute, there is also a new attribute filter on the <b>Get-ChildItem<\/b> cmdlet. This filter accepts a simple filter language. This filter language accepts the following operators:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; Operator<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; Meaning<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; +<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; And<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; !<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; Not<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; ,<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>&nbsp; Or<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>When using the attribute filters, do not include spaces between operators (although a space after the comma ( ,&nbsp;) operator is acceptable).<\/p>\n<p>The following command lists all hidden, system files, but not read-only files.<\/p>\n<p style=\"padding-left: 30px\">dir -File -force -Attributes !readonly<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>Keep in mind, that the attribute aliases listed earlier only work with the three switches: <b>ReadOnly<\/b>, <b>System<\/b>, and <b>Hidden<\/b>, and that they do not work in attribute filtering.<\/p>\n<p>The following command lists <b>System<\/b> files that are also <b>ReadOnly<\/b><i>. <\/i><\/p>\n<p style=\"padding-left: 30px\">dir -File -force -Attributes System+Readonly<\/p>\n<p>The <b>Attributes<\/b><i> <\/i>parameter accepts any of the system.io.fileattributes enumeration values. To see these enumeration names, I used the command shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [enum]::getnames([system.io.fileattributes])<\/p>\n<p style=\"padding-left: 30px\">ReadOnly<\/p>\n<p style=\"padding-left: 30px\">Hidden<\/p>\n<p style=\"padding-left: 30px\">System<\/p>\n<p style=\"padding-left: 30px\">Directory<\/p>\n<p style=\"padding-left: 30px\">Archive<\/p>\n<p style=\"padding-left: 30px\">Device<\/p>\n<p style=\"padding-left: 30px\">Normal<\/p>\n<p style=\"padding-left: 30px\">Temporary<\/p>\n<p style=\"padding-left: 30px\">SparseFile<\/p>\n<p style=\"padding-left: 30px\">ReparsePoint<\/p>\n<p style=\"padding-left: 30px\">Compressed<\/p>\n<p style=\"padding-left: 30px\">Offline<\/p>\n<p style=\"padding-left: 30px\">NotContentIndexed<\/p>\n<p style=\"padding-left: 30px\">Encrypted<\/p>\n<p style=\"padding-left: 30px\">IntegrityStream<\/p>\n<p style=\"padding-left: 30px\">NoScrubData<\/p>\n<p>The ability to use the <b>FileAttributes<\/b> enumeration is very powerful. For example, I can find temporary files by using the following command (<b>gci<\/b> is an alias for <b>Get-ChildItem<\/b>. <b>Ea<\/b> is an alias for the <b>ErrorAction<\/b> parameter, and 0 means to ignore errors).<\/p>\n<p style=\"padding-left: 30px\">gci -Recurse -Attributes temporary -ea 0<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6740.hsg-10-10-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6740.hsg-10-10-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To look at the attributes of the file, I use the following command.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Item &#8216;C:\\data\\ScriptingGuys\\ScriptingGames_2012\\Scripting-Games-2012_comp<\/p>\n<p style=\"padding-left: 30px\">ete &#8211; Copy.png&#8217; | select name, attributes<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Attributes<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Scripting-Games-2012_compete &#8211; Copy.png&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Archive, Temporary<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell 3.0 for file attribute filtering. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I will be at the Copenhagen Windows PowerShell User Group in a about a month. Teresa has been corresponding with Windows PowerShell MVP, Claus T. Nielsen, about the agenda for the meeting. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,362,3,12,45],"class_list":["post-4846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-powershell-3","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell 3.0 for file attribute filtering. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I will be at the Copenhagen Windows PowerShell User Group in a about a month. Teresa has been corresponding with Windows PowerShell MVP, Claus T. Nielsen, about the agenda for the meeting. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4846"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4846\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}