PowerShell Cleanup Subfolders

By: Cam Wohlfeil
Published: 2018-02-07 0000 EST
Category: Solutions
Tags: powershell

Want to delete a bunch of empty subfolders? Or maybe move all the files first? In PowerShell it's super easy.

# Move all files in subfolders of source to destination
Get-ChildItem -Path source -Recurse -File | Move-Item -Destination destination
# Delete all empty folders in source
Get-ChildItem -Path source -Recurse -Directory | Remove-Item

Note: You can use -Recurse on Remove-Item to remove subfolders of subfolders. Also, you can use -Force so it will delete folders that still have files in them (but you really shouldn't).

More Info: