PowerShell for Windows Admins

May 7 2013   3:21PM GMT

Scripting Games–new Get-ChildItem parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: