FatalBulletHit

Empty Folder Remover (Advanced)

Apr 21st, 2019 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://pastebin.com/mRhrVdGA
  2. # Check out this pastebin entry for a more simplistic version: https://pastebin.com/fmBzGg2W
  3.  
  4. $Host.UI.RawUI.WindowTitle = 'Empty Folder Remover (Advanced)'
  5. [System.String]$Target ='C:\'
  6. [System.Boolean]$Silent = $False
  7. [System.Int64]$DirCounter = 0
  8. [System.Int64]$FailCounter = 0
  9. [System.Int64]$SkipCounter = 0
  10. $Log = New-Object System.Collections.ArrayList
  11. $Error.Clear()
  12.  
  13. # Offer to run script as administrator if current enviroment has no administrator rights
  14.  
  15. if (!(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  16.  
  17.     Write-Host -ForegroundColor Yellow 'Enviroment has no administrator rights! Run as administrator?'
  18.     Write-Host '[Y]es / [N]o'
  19.    
  20.     while (($UserInput = Read-Host)[0] -ne 'y' -and ($UserInput = Read-Host)[0] -ne 'n') {
  21.    
  22.         Write-Host -ForegroundColor Red ("'{0}' is not a valid input!" -f $UserInput)
  23.    
  24.     }
  25.    
  26.     if ($UserInput[0] -eq 'y') {
  27.    
  28.         Start-Process PowerShell -ArgumentList ('-NoExit & {0}' -f $PSCommandPath) -Verb RunAs
  29.         Stop-Process -Id $PID
  30.    
  31.     }
  32.    
  33. } else {
  34.  
  35.     Write-Host -ForegroundColor Yellow "Enviroment has administrator rights!`n"
  36.     $Log.Add('Enviroment has administrator rights!') > $null
  37.  
  38. }
  39.  
  40. # Offer to run script silent (no log on screen and no log output)
  41.  
  42. Write-Host -ForegroundColor Yellow 'Run silent?'
  43. Write-Host '[Y]es / [N]o'
  44.  
  45. while (($UserInput = Read-Host)[0] -ne 'y' -and ($UserInput = Read-Host)[0] -ne 'n') {
  46.  
  47.     Write-Host -ForegroundColor Red ("'{0}' is not a valid input!" -f $UserInput)
  48.  
  49. }
  50.  
  51. if ($UserInput[0] -eq 'y') {
  52.  
  53.     $Silent = $True
  54.  
  55. }
  56.  
  57. # Deleting empty directories
  58.  
  59. Get-ChildItem -LiteralPath $Target -Recurse -Force -Directory -ErrorAction Ignore | ForEach-Object {
  60.    
  61.     Try {
  62.    
  63.         Get-ChildItem -LiteralPath $_.FullName -Force -ErrorAction Stop | Select-Object -First 1 > $null
  64.    
  65.     }
  66.    
  67.     Catch {
  68.    
  69.         if (!$Silent) {
  70.        
  71.             Write-Host -ForegroundColor Red $_.Exception.Message
  72.             $Log.Add($_Exception.Message) > $null
  73.            
  74.         }
  75.     }
  76.    
  77.     if (!$Error) {
  78.    
  79.         if ((Get-ChildItem -LiteralPath $_.FullName -Recurse -Force -File | Select-Object -First 1).Count -eq 0) {
  80.            
  81.             Try {
  82.            
  83.                 Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction Stop
  84.            
  85.             }
  86.            
  87.             Catch {
  88.            
  89.                 if (!$Silent -and $_.Exception.Message -eq 'You do not have sufficient access rights to perform this operation.') {
  90.                
  91.                     Write-Host -ForegroundColor Red ("Not sufficient access rights to remove '{0}'!" -f $_.FullName)
  92.                     $Log.Add(("Not sufficient access rights to remove '{0}'!" -f $_.FullName)) > $null
  93.                    
  94.                 } elseif (!$Silent) {
  95.                
  96.                     Write-Host -ForegroundColor Red $_.Exception.Message
  97.                     $Log.Add($_.Exception.Message) > $null
  98.                
  99.                 }
  100.             }              
  101.            
  102.             if (!$Error) {
  103.            
  104.                 $DirCounter++
  105.                
  106.                 if (!$Silent) {
  107.                
  108.                     Write-Host ("Removed '{0}'" -f $_.FullName)
  109.                     $Log.Add(("Removed '{0}'" -f $_.FullName)) > $null
  110.                
  111.                 }
  112.            
  113.             } else {
  114.            
  115.                 $FailCounter++
  116.            
  117.             }
  118.         }
  119.        
  120.     } else {
  121.    
  122.         $SkipCounter++
  123.    
  124.     }
  125.    
  126.     $Error.Clear()
  127.  
  128. }
  129.  
  130. # Summary
  131.  
  132. Write-Host -ForegroundColor Green ("`nDone!`n`nRemoved {0} empty directories!`n" -f $DirList.Count)
  133. $Log.Add(("`nDone!`n`nRemoved {0} empty directories!`n" -f $DirList.Count)) > $null
  134.  
  135. if ($SkipCounter -gt 0) {
  136.  
  137.     Write-Host -ForegroundColor Yellow ("Skipped {0} directories due to access rights!`n" -f $SkipCounter)
  138.     $Log.Add(("Skipped {0} directories due to access rights!`n" -f $SkipCounter)) > $null
  139.  
  140. }
  141.  
  142. if ($FailCounter -gt 0) {
  143.  
  144.     Write-Host -ForegroundColor Yellow ("Failed to delete {0} empty directories!`n" -f $FailCounter)
  145.     $Log.Add(("Failed to delete {0} empty directories!`n" -f $FailCounter)) > $null
  146.  
  147. }
  148.  
  149. Write-Host ("Log has been saved to '{0}'" -f ($LogName = (Get-Date -UFormat %Y-%m-%d_%T)))
  150.  
  151. $Log | Add-Content -LiteralPath ('{0}\Documents\EmptyFolderRemover_Log{1}' -f $Home, $LogName)
Add Comment
Please, Sign In to add comment