Count Child Directories with PowerShell

rpalo profile image Ryan Palo ・1 min read

At work, I received a supplier survey from a customer asking how many customers we have. My first thought was, "Ugh! I'm going to have to go into our system and figure out where those reports live and make sure I'm getting the right numbers from the right places." But then I remembered our server. And I remembered that I'm a programmer and we don't have to stand for things like manually counting.

Bada bing, bada boom:

gci -dir | measure

# Which is PowerShell auto-aliasing goodness for:
Get-ChildItem -Directory | Measure-Object

For good measure, we can get the job done in Bash the same way:

ls -d */ | wc -l

# I've used ls -d here rather than find because I wanted to skip hidden dirs

I love the CLI so much!


Editor guide