Windows PowerShell One-Liner: List all the subdirectories in the current directory

Windows PowerShell One-Liner: List all the subdirectories in the current directory

  • Comments 6

dir | where {$_.PsIsContainer}

Alternatively you could:

dir |where {$_.mode -match "d"}

Jeffrey Snover
Windows PowerShell Architect

Leave a Comment
  • Please add 7 and 6 and type the answer here:
  • Post
  • I have a number of dir* functions that I create in my profile:

    function dir   { get-childitem $args -ea silentlycontinue | sort @{e={$_.PSIsContainer}; asc=$false},@{e={$_.Name}; asc=$true} }
    function dirw  { get-childitem $args -ea silentlycontinue | sort @{e={$_.PSIsContainer}; asc=$false},@{e={$_.Name}; asc=$true} | format-wide }
    function dirs  { get-childitem $args -ea silentlycontinue | sort Length }
    function dirt  { get-childitem $args -ea silentlycontinue | sort LastWriteTime }
    function dird  { get-childitem $args -ea silentlycontinue | where { $_.PSIsContainer } }
  • Thanks Keith!

    I've now added these to my startup profile.  I changed dirw to do autosizing to allow use of wide screens:

    function dirw  { get-childitem $args -ea silentlycontinue | sort @{e={$_.PSIsContainer}; asc=$false},@{e={$_.Name}; asc=$true} | format-wide -auto}

    (I love community efforts - they make me smarter faster!)

    Jeffrey Snover [MSFT]
    Windows PowerShell Architect
  • This is my oneliner that gets items in the current directory that are older than the newest item. I use this to backup old IIS logs.

    dir | where { !$_.PsIsContainer } | where { $_.Name -ne (dir | where { !$_.PSIsContainer } | sort LastWriteTime -descending | select -first 1 | %{$_.Name} ) } | %{ $_.FullName }
  • This is ~one~ of my biggest gripes against powershell:

    get-childitem | where {$_.mode -match "d"}

    versus

    dir /AD

    One of them is quick and efficient to type; the other tedious, wordy, aggravating.  

    If everyone has to alias everything just to work efficiently on a command line, then really: what benefit is the overly-lengthy Verb-Noun syntax coupled with long switches ("silentlycontinue"?!)?  

    As a system admin who can also program in a handful of languages, I feel Powershell lost sight of being a command line interface for SAs.

  • I agree with Eric.  There should be some shortcuts or something.  I'm having trouble getting a certain property from a directory after doing this... should be simpler.  I know it can be done, but why does it have to be so complicated?

  • How do you get PW to delete the tmp directories stored under c:\user\userID\AppData\local\temp

    where userID is the user's directory?

Page 1 of 1 (6 items)