Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://pastebin.com/mRhrVdGA
- # Check out this pastebin entry for a more simplistic version: https://pastebin.com/fmBzGg2W
- $Host.UI.RawUI.WindowTitle = 'Empty Folder Remover (Advanced)'
- [System.String]$Target ='C:\'
- [System.Boolean]$Silent = $False
- [System.Int64]$DirCounter = 0
- [System.Int64]$FailCounter = 0
- [System.Int64]$SkipCounter = 0
- $Log = New-Object System.Collections.ArrayList
- $Error.Clear()
- # Offer to run script as administrator if current enviroment has no administrator rights
- if (!(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
- Write-Host -ForegroundColor Yellow 'Enviroment has no administrator rights! Run as administrator?'
- Write-Host '[Y]es / [N]o'
- while (($UserInput = Read-Host)[0] -ne 'y' -and ($UserInput = Read-Host)[0] -ne 'n') {
- Write-Host -ForegroundColor Red ("'{0}' is not a valid input!" -f $UserInput)
- }
- if ($UserInput[0] -eq 'y') {
- Start-Process PowerShell -ArgumentList ('-NoExit & {0}' -f $PSCommandPath) -Verb RunAs
- Stop-Process -Id $PID
- }
- } else {
- Write-Host -ForegroundColor Yellow "Enviroment has administrator rights!`n"
- $Log.Add('Enviroment has administrator rights!') > $null
- }
- # Offer to run script silent (no log on screen and no log output)
- Write-Host -ForegroundColor Yellow 'Run silent?'
- Write-Host '[Y]es / [N]o'
- while (($UserInput = Read-Host)[0] -ne 'y' -and ($UserInput = Read-Host)[0] -ne 'n') {
- Write-Host -ForegroundColor Red ("'{0}' is not a valid input!" -f $UserInput)
- }
- if ($UserInput[0] -eq 'y') {
- $Silent = $True
- }
- # Deleting empty directories
- Get-ChildItem -LiteralPath $Target -Recurse -Force -Directory -ErrorAction Ignore | ForEach-Object {
- Try {
- Get-ChildItem -LiteralPath $_.FullName -Force -ErrorAction Stop | Select-Object -First 1 > $null
- }
- Catch {
- if (!$Silent) {
- Write-Host -ForegroundColor Red $_.Exception.Message
- $Log.Add($_Exception.Message) > $null
- }
- }
- if (!$Error) {
- if ((Get-ChildItem -LiteralPath $_.FullName -Recurse -Force -File | Select-Object -First 1).Count -eq 0) {
- Try {
- Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction Stop
- }
- Catch {
- if (!$Silent -and $_.Exception.Message -eq 'You do not have sufficient access rights to perform this operation.') {
- Write-Host -ForegroundColor Red ("Not sufficient access rights to remove '{0}'!" -f $_.FullName)
- $Log.Add(("Not sufficient access rights to remove '{0}'!" -f $_.FullName)) > $null
- } elseif (!$Silent) {
- Write-Host -ForegroundColor Red $_.Exception.Message
- $Log.Add($_.Exception.Message) > $null
- }
- }
- if (!$Error) {
- $DirCounter++
- if (!$Silent) {
- Write-Host ("Removed '{0}'" -f $_.FullName)
- $Log.Add(("Removed '{0}'" -f $_.FullName)) > $null
- }
- } else {
- $FailCounter++
- }
- }
- } else {
- $SkipCounter++
- }
- $Error.Clear()
- }
- # Summary
- Write-Host -ForegroundColor Green ("`nDone!`n`nRemoved {0} empty directories!`n" -f $DirList.Count)
- $Log.Add(("`nDone!`n`nRemoved {0} empty directories!`n" -f $DirList.Count)) > $null
- if ($SkipCounter -gt 0) {
- Write-Host -ForegroundColor Yellow ("Skipped {0} directories due to access rights!`n" -f $SkipCounter)
- $Log.Add(("Skipped {0} directories due to access rights!`n" -f $SkipCounter)) > $null
- }
- if ($FailCounter -gt 0) {
- Write-Host -ForegroundColor Yellow ("Failed to delete {0} empty directories!`n" -f $FailCounter)
- $Log.Add(("Failed to delete {0} empty directories!`n" -f $FailCounter)) > $null
- }
- Write-Host ("Log has been saved to '{0}'" -f ($LogName = (Get-Date -UFormat %Y-%m-%d_%T)))
- $Log | Add-Content -LiteralPath ('{0}\Documents\EmptyFolderRemover_Log{1}' -f $Home, $LogName)
Add Comment
Please, Sign In to add comment