DEV Community

Ryan Palo
Ryan Palo

Posted on

Count Child Directories with PowerShell

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

I love the CLI so much!

Top comments (0)