Welcome to MSDN Blogs Sign in | Join | Help

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

dir | where {$_.PsIsContainer}

Alternatively you could:

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

Jeffrey Snover
Windows PowerShell Architect

Published Saturday, June 24, 2006 12:58 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

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

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 } }
Saturday, June 24, 2006 12:56 AM by KeithH

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

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
Saturday, June 24, 2006 11:46 AM by Monad Team

# my $.5

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 }
Wednesday, June 28, 2006 6:17 AM by b3ardman

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

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.

Sunday, April 27, 2008 5:19 PM by Eric

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

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?

Friday, May 23, 2008 3:08 PM by Jason

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker