Use PowerShell 3.0 to Filter Files Based on Advanced Attributes

Use PowerShell 3.0 to Filter Files Based on Advanced Attributes

  • Comments 1
  • Likes

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. It is official, I will be talking about the new features in Windows PowerShell 3.0. The initial details are available on the Copenhagen Windows PowerShell User Group 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.

Using Get-ChildItem built-in aliases

In Finding Read-Only and System Files Using PowerShell 3.0, I talked about using the new switches to find hidden, system, or read-only files by using the Get-ChildItem cmdlet.

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.

dir -File -ReadOnly –Force

But if I want to, I can shorten the command by using an alias, ar, in place of readonly. This command is shown here.

dir -File -ar –Force

On my computer, the previous command saves one key stroke. This is because I use tab completion, and recurse appears when I press r<tab> the first time. When I press the second <tab> I get the Recurse parameter. Therefore, I type the following:

R<tab><tab>

This equals three key strokes. On the other hand, by using the alias, I only type the following:

Ar

This is two key strokes. It is not quite such a big deal. In addition, the alias ar 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.

Note   The Force parameter is not required when using the ReadOnly 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 Force parameter to see the file.

The alias for the Hidden switch, it is ah. The following command retrieves hidden files.

dir -File -ah

The alias for the System switch is as. The following command retrieves system files.

dir -File -Force –as

Note   The pattern is a for attribute, and then the first letter of the attribute name, for example: r, h, s.

Using attribute filters

In addition to having several new switches that simplify finding files with a particular attribute, there is also a new attribute filter on the Get-ChildItem cmdlet. This filter accepts a simple filter language. This filter language accepts the following operators:

  Operator

  Meaning

  +

  And

  !

  Not

  ,

  Or

When using the attribute filters, do not include spaces between operators (although a space after the comma ( , ) operator is acceptable).

The following command lists all hidden, system files, but not read-only files.

dir -File -force -Attributes !readonly

Note   Keep in mind, that the attribute aliases listed earlier only work with the three switches: ReadOnly, System, and Hidden, and that they do not work in attribute filtering.

The following command lists System files that are also ReadOnly.

dir -File -force -Attributes System+Readonly

The Attributes parameter accepts any of the system.io.fileattributes enumeration values. To see these enumeration names, I used the command shown here.

PS C:\> [enum]::getnames([system.io.fileattributes])

ReadOnly

Hidden

System

Directory

Archive

Device

Normal

Temporary

SparseFile

ReparsePoint

Compressed

Offline

NotContentIndexed

Encrypted

IntegrityStream

NoScrubData

The ability to use the FileAttributes enumeration is very powerful. For example, I can find temporary files by using the following command (gci is an alias for Get-ChildItem. Ea is an alias for the ErrorAction parameter, and 0 means to ignore errors).

gci -Recurse -Attributes temporary -ea 0

The command and the output associated with the command are shown here.

Image of command output

To look at the attributes of the file, I use the following command.

PS C:\> Get-Item 'C:\data\ScriptingGuys\ScriptingGames_2012\Scripting-Games-2012_comp

ete - Copy.png' | select name, attributes

 

Name                                                                      Attributes

----                                                                      ----------

Scripting-Games-2012_compete - Copy.png                           Archive, Temporary

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    I would never ever have guessed these parameter aliases!

    Well, you may say: "Read the help!" and RIGHT! There it is documented ...

    The powershell-way of detecting them might be:

    (Get-Command Get-ChildItem).Parameters.values | where aliases | select name, aliases

    btw: Who has chosen "s" as alias for "recurse"? ( It must have been a UNIX freak, I suppose :-)

    Klaus.