Skip to content

Instantly share code, notes, and snippets.

@jimdiroffii
Last active September 14, 2020 14:50
Show Gist options
  • Save jimdiroffii/81cc075a2ecb0f5f36ad0cbf5fbd90f6 to your computer and use it in GitHub Desktop.
Save jimdiroffii/81cc075a2ecb0f5f36ad0cbf5fbd90f6 to your computer and use it in GitHub Desktop.
PowerShell Tail Recursion for Removing Empty Directories
$tailRecursion = {
param($Path)
foreach ($childDirectory in Get-ChildItem -Force -LiteralPath $Path -Directory) {
& $tailRecursion -Path $childDirectory.FullName
}
$currentChildren = Get-ChildItem -Force -LiteralPath $Path
$isEmpty = $currentChildren -eq $null
if ($isEmpty) {
Write-Verbose "Removing empty folder at path '${Path}'." -Verbose
Remove-Item -Force -LiteralPath $Path
}
}
@jimdiroffii
Copy link
Author

jimdiroffii commented Sep 14, 2020

Usage: & $tailRecursion -Path .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment