PowerShell for Windows Admins

Feb 28 2014   11:55AM GMT

File or Folder?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This is a common type of use for Get-ChildItem:

Get-ChildItem -Path c:\temp

However, you could get files or folders returned.  Very often you just want to see the files

In PowerShell 1.0 & 2.0 you had to do this:

Get-ChildItem -Path c:\temp   | where {-not $_.PSIsContainer}

If you want to see the subfolders only then you reverse the condition:

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

In PowerShell 3.0 Get-ChildItem got some new parameters –File and –Directory. They display files only or folders only respectively. The default action is still to display files and folders.

Get-ChildItem -Path c:\temp  -File  Get-ChildItem -Path c:\temp  -Directory

I still see a lot of people making more work for themselves because they don’t use or don’t know about the new 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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: