PowerShell

PowerShell: Search for Empty Folders (and delete them)

We live in times of Big Data. Too much data in too short a time. You may have empty folders in your environment and if you want to search for them and delete them then you are at the right place. We will do just that.

Run this little script below to remove all empty folders.

At the top you need to specify the path to the root folder. In my case this is C:\Data. You will be asked to confirm the deletion.

# Get a list of all folders
$rootfolder = "C:\Data"
$folders = Get-ChildItem -Path $rootfolder -Directory -Recurse

# Filter out the empty folders
$emptyFolders = $folders | Where-Object {$_.GetFileSystemInfos().Count -eq 0}

# Delete the empty folders
$emptyFolders | Remove-Item -Force -Verbose

And here is the code in Action.

We have now cleaned up a bit. That’s good.

3 replies »

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.