«

»

Apr 02

Powershell Tips of the week #4 – Get-ChildItem – Only show directories

Here’s quick tip on how to make Get-ChildItem only return directories.

With Powershell v3, there is a new parameter that allow you to only get directories:

Get-ChildItem C:\ -directory


Result:
Directory: C:\
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/13/2009  11:20 PM            PerfLogs
d-r--          4/2/2013   8:31 AM            Program Files
d-r--         3/27/2013   9:03 AM            Program Files (x86)
d-r--         1/17/2013   2:17 PM            Users
d----         3/27/2013   9:01 AM            Windows

 
 
Since the parameter “-directory” does not exist in Powershell v2 you will need to use the attribute “PSIsContainer” to filer the directories:

Get-ChildItem c:\ | Where-Object{$_.PSIsContainer -eq $true}


Result:
Directory: C:\
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/13/2009  11:20 PM            PerfLogs
d-r--          4/2/2013   8:31 AM            Program Files
d-r--         3/27/2013   9:03 AM            Program Files (x86)
d-r--         1/17/2013   2:17 PM            Users
d----         3/27/2013   9:01 AM            Windows

1 comment

  1. Glenn

    You can shorten the above to:

    gci c:\ | ?{$_.PSIsContainer}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>