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.
To answer the second part of your question, of getting the leaf folder count, just modify the where object clause to add a non-recursive search of each directory, getting only those that return a count of 0:
it looks a little cleaner if you can use powershell 3.0:
Get the path child items with recourse option, pipe it to filter only containers, pipe again to measure item count
Another option:
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.As a one-liner:
In PowerShell 3.0 you can use the Directory switch:
This is a pretty good starting point:
However, I suspect that this will include
.zip
files in the count. I'll test that and try to post an update...EDIT: Have confirmed that zip files are not counted as containers. The above should be fine!