Remove Empty Directories
Function Remove-IfEmpty {
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(ValueFromPipeline)] $dir
)
process {
if (
(Test-Path $dir.fullname -PathType container) -and
((ls $dir.fullname -rec -file | measure | select -expand count) -eq 0)
) {
rm $_.fullname
}
}
}
ls -rec -dir | Remove-IfEmpty -WhatIf
ls -rec -dir | Remove-IfEmpty -Confirm
ls -dir | select -first 1 | Remove-IfEmpty
Minified:
Function Remove-IfEmpty {[CmdletBinding(SupportsShouldProcess)] Param([Parameter(ValueFromPipeline)] $dir); process {if ((Test-Path $dir.fullname -PathType container) -and ((ls $dir.fullname -rec -file | measure | select -expand count) -eq 0)) {rm $_.fullname}}}