I wrote the following script to recursively remove any folders that are empty. I have tested this in a sandbox and it appears to be safe. That is, it will only remove a folder if it is empty and it will only navigate within subfolders within the starting parent folder and won't inadvertently go further up in the directory.
So, if you could take a quick look at my script and tell me if you see any dangers, I would appreciate. Thank you!
$isEmpty=1
$iteration=0
while ($isEmpty) {
$isEmpty=0
$iteration++
get-childitem -Directory -Force -recurse | ForEach-Object {
$count=(Get-ChildItem -Path $_.FullName -Force).count
if ($count -eq 0) {
$isEmpty=1
Write-Host "$iteration`t$count`t$_"
$path="\\?\"+$_.FullName
$folder= Get-item -Path $path
$folder.Attributes = $folder.Attributes -band -bnot [System.IO.FileAttributes]::ReadOnly
$folder.Attributes = $folder.Attributes -band -bnot [System.IO.FileAttributes]::Hidden
Remove-Item -Force -LiteralPath $path
}
}
}
While
1and0do work as implicit Boolean values, it's better to use Booleans explicitly, i.e.$trueand$false:$isEmpty = $true,$isEmpty = $falseIf you know paths to be literal (verbatim) paths rather than wildcard expressions, use
-LiteralPathinstead of-Path.The
-LiteralPathrecommendation applies equally, but, more importantly:-Forceis needed to target hidden items withGet-ItemorGet-ChildItem.You do not need to explicitly clear the
HiddenandReadOnlyattributes from file-system items in order forRemove-Itemto remove them - using-Forceis sufficient.Taking a step back:
You can avoid multiple traversals of your folder subtree if you process directories (folders) bottom up, i.e. if you start with the leaf directories in the directory subtree and iteratively traverse upward:
Note:
The
-WhatIfcommon parameter in the command above previews the operation. Remove-WhatIfand re-execute once you're sure the operation will do what you want.That said, given the iterative nature of your task,
-WhatIfwill only show you the empty leaf directories that would get deleted and not any of their ancestors that would also get deleted as a result of deleting any leaves.-WhatIf) will tell you the actual results.