Finding Read-Only and System Files by Using PowerShell 3.0


Summary: Learn how to use new parameters with the enhanced Get-ChildItem cmdlet in Windows PowerShell 3.0.

Microsoft Scripting Guy, Ed Wilson, is here. There are a number of really cool new things in Windows PowerShell 3.0. Some of these make my life a lot easier, and yet they are not the “big” things. I have written about one of my favorite thingsand that is the “automatic foreach” feature that permits me to directly access a property from a collection. I use the feature nearly every day.

Enhanced file and folder work

One new feature that has received virtually no press, is the new switches available for the Get-ChildItem cmdlet. The new switches permit simplified code. For example, in Windows PowerShell 1.0 and in Windows PowerShell 2.0, if I wanted to get a list of directories, I needed to write code similar to the code shown here.

dir | where {$_.psiscontainer}

To find the files (including hidden files) I would need to use either of the following types of commands.

dir -force | where {!($_.psiscontainer)}

dir -Force| where {!$_.psiscontainer}

With no improvements to the Get-ChildItem cmdlet (dir is an alias) in Windows PowerShell 3.0, I can simplify the code to find directories to something such as is shown here.

dir | where psiscontainer

New switches for the Get-ChildItem cmdlet

Improvements to the Get-ChildItem cmdlet simplify the code required to find files and directories (folders) by permitting direct access via the cmdlet. For example, to find directories using the Get-ChildItem (dir is an alias), I use the command shown here.

dir –Directory

To find files, I use the command shown here (use the Force parameter to show hidden and system files.)

dir -File –Force

The use of these two commands is shown here.

                Image of command output

To find all of the read-only files by using Windows PowerShell 2.0 or Windows PowerShell 1.0 required using either the Match operator or the Like operator in a Where-Object command. This type of command is shown here.

dir -force | where {!$_.psiscontainer -and $_.mode -match ‘r’}

Although this command is not too cryptic for me (but then I have been using and writing about Windows PowerShell since it was called Monad), it takes a lot of time to write such a command–and for people just coming to Windows PowerShell, a command such as this one is just ridiculous. In Windows PowerShell 3.0 I can shorten the previous command to the one shown here.

dir -force -File -Attributes readonly

Not only is the command shorter, but it is much easier to read. The command actually makes sense! The two previous commands, and the output associated with each is shown in the figure that follows.

Image of command output

Using attribute switches

To simplify working with the Get-ChildItem cmdlet, Windows PowerShell 3.0 adds several new attribute switches. The switches are: ReadOnly, Hidden, and System. Therefore, instead of searching for files with the ReadOnly attribute as I did in the previous command, I can look specifically for read-only files by using the ReadOnly switch. The command is shown here.

dir -force -File -ReadOnly

To find only hidden files, the command would change to the one shown here.

dir -Force -File –Hidden

Normally, when working with files, I have to specify the Force parameter to see the hidden and system files. This particular behavior, although it makes sense, is also a terrific potential for error because the Force parameter is, unfortunately, not implemented in all cmdlets in the same way. Therefore, for some cmdlets you use the Force parameter, and for other cmdlets you do other things. Luckily, with the new Hidden and System switches, this confusion is removed. For example, the following command also lists all the hidden files.

dir -File –Hidden

The same behavior does not seem to appear when I list the system files. The reason is that while the System switch lists system files, all of the system files on my computer are also hidden; and therefore, I will need to use either the Force parameter to show hidden files, or use the Hidden switch.

 PS C:\> dir -File -System -Force


    Directory: C:\



Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-arhs         7/14/2012   6:03 AM     398146 bootmgr

-a-hs          6/2/2012  10:30 AM          1 BOOTNXT

-a-hs         9/25/2012   5:49 PM 6773993472 hiberfil.sys

-a-hs         9/25/2012   5:49 PM 4831838208 pagefile.sys

-a-hs         9/25/2012   5:49 PM  268435456 swapfile.syss seen here.

Join me tomorrow when I will talk about working with file attributes via the Get-ChildItem cmdlet in Windows PowerShell 3.0.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at or share you solutions and questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon