powershell how to removve only empty direcoties
To remove only empty directories using PowerShell, you can use the Remove-Item
cmdlet with the -Directory
and -Recurse
parameters. Here's the command you can use:
Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 } | Remove-Item -Recurse
Let me break down the command for you:
Get-ChildItem -Directory
retrieves all the directories in the current location.Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
filters the directories and selects only those with no files or subdirectories inside.Remove-Item -Recurse
removes the selected empty directories.
Please note that this command will permanently delete the empty directories, so use it with caution.
I hope this helps!