Counting folders with Powershell

powershell, scripting, shell, windows

Solution

You can use `get-childitem -recurse` to get all the files and folders in the current folder.

Pipe that into `Where-Object` to filter it to only those files that are containers.

$files = get-childitem -Path c:\temp -recurse 
$folders = $files | where-object { $_.PSIsContainer }
Write-Host $folders.Count

As a one-liner:

(get-childitem -Path c:\temp -recurse | where-object { $_.PSIsContainer }).Count

Problem

Does anybody know a powershell 2.0 command/script to count all folders and subfolders (recursive; no files) in a specific folder ( e.g. the number of all subfolders in C:\folder1\folder2)? In addition I also need also the number of all "leaf"-folders. in other words, I only want to count folders, which don't have subolders.

Original source