Finding Empty Folders

by Jul 23, 2009

To find out all folders that contain no files, you can use this line:

dir -recurse | Where-Object { $_.PSIsContainer } |
Where-Object { $_.GetFiles().Count -eq 0 } |
ForEach-Object { $_.FullName }

It first retrieves all folders recursively and then for each folder calls its GetFiles() method. Next, it only lets those folders pass the pipeline where GetFiles() returned 0 elements. Finally, the folder path is emitted.

If you'd rather want to create a list of completely empty folders (no subfolders either), simply add another filter like this:

dir -recurse | Where-Object { $_.PSIsContainer } |
Where-Object { $_.GetFiles().Count -eq 0 } |
Where-Object { $_.GetDirectories().Count -eq 0 } |
ForEach-Object { $_.FullName }