Scripting Games–new Get-ChildItem parameters

One improvement that came with PowerShell v3 is the –File and –Directory parameters on Get-ChildItem

If I run this

Get-ChildItem -Path c:\mydata

I will get a mixture of directories and files

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        19/11/2012     20:19            Delivery
d----        26/02/2013     19:24            Demo
d----        05/05/2013     11:26            ScriptingGames 2013
d-r--        07/05/2013     18:17            SkyDrive
d----        24/01/2013     20:08            Summit NA 2012
-a---        06/05/2013     15:26    1336320 2013May_ErrorHandling.doc

If I add the –Recurse parameter it gets worse – I know I’ve got a lot of files and directories in here.

In PowerShell v2 you could separate the directories and files by using PSISContainer

Get-ChildItem -Path c:\mydata | where {$_.PSIsContainer}
Get-ChildItem -Path c:\mydata | where {!$_.PSIsContainer}

will give you the directories only and files only respectively.

It gets easier in PowerShell v3

Get-ChildItem -Path c:\mydata -Directory
Get-ChildItem -Path c:\mydata –File

simple and obvious when you read it.

if you are using PowerShell v3 don’t forget these parameters

Leave a Reply