Last active
September 14, 2020 14:50
-
-
Save jimdiroffii/81cc075a2ecb0f5f36ad0cbf5fbd90f6 to your computer and use it in GitHub Desktop.
PowerShell Tail Recursion for Removing Empty Directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
& $tailRecursion -Path .