Cogger

Delete-Empty-Folders.ps1

Jun 17th, 2022 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ErrorActionPreference = 'SilentlyContinue'
  2. ##  Function to Delete empty folders
  3. $tailRecursion = {
  4.     param(
  5.         $Path
  6.     )
  7.     foreach ($childDirectory in Get-ChildItem -Force -LiteralPath $Path -Directory) {
  8.         & $tailRecursion -Path $childDirectory.FullName
  9.     }
  10.     $currentChildren = Get-ChildItem -Force -LiteralPath $Path
  11.     $isEmpty = $currentChildren -eq $null
  12.     if ($isEmpty) {
  13.         Write-Verbose "Removing empty folder at path '${Path}'." -Verbose
  14.         Remove-Item -Force -LiteralPath $Path
  15.     }
  16. }
  17. cls
  18.  
  19.     $Location=$(get-location).Path;
  20.     & $tailRecursion -Path "$Location"
  21.  
  22.  
  23.  
Add Comment
Please, Sign In to add comment