# Version date: 2026-08-13 15:56 (CEST) # ------------------------------------------------------------ # Deletes empty folders in the root directory of a chosen drive. A folder # counts as "empty" if it contains no visible files anywhere inside it # (recursively) - hidden files (e.g. a stray desktop.ini Windows may add # to a customized folder) are ignored when deciding this, since they # don't represent real content. # # Only root-level folders on the chosen drive are considered - nothing # nested deeper is evaluated or touched. # ------------------------------------------------------------ Write-Host "" Write-Host "This script deletes empty folders." $DriveInput = Read-Host "Which drive do you wish to process? (e.g. E)" # Normalize whatever was typed (E, e, E:, e:\, etc.) to a plain "E:\" form. $DriveLetter = $DriveInput.Trim().TrimEnd(':', '\').ToUpper() $DrivePath = "$DriveLetter`:\" if ($DriveLetter -ieq "C") { Write-Host "WARNING: This script will not run on the C: drive - aborting." exit } if (-not (Test-Path $DrivePath)) { Write-Host "ERROR: Drive $DrivePath was not found." exit } # -------------------------------------------------------------------------- # Find every root-level folder whose visible (non-hidden) content, summed # recursively, is 0 bytes. # -------------------------------------------------------------------------- $EmptyFolders = Get-ChildItem $DrivePath -Directory -ErrorAction SilentlyContinue | ForEach-Object { $VisibleSize = ( Get-ChildItem $_.FullName -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { -not ($_.Attributes -band [System.IO.FileAttributes]::Hidden) } | Measure-Object Length -Sum ).Sum if (-not $VisibleSize) { [PSCustomObject]@{ Name = $_.Name FullName = $_.FullName LastWriteTime = $_.LastWriteTime } } } if (-not $EmptyFolders) { Write-Host "" Write-Host "No empty folders found in $DrivePath." exit } Write-Host "" Write-Host "========================================" Write-Host "Empty folders in $DrivePath" Write-Host "========================================" $EmptyFolders | Select-Object Name, @{ Name = "LastChanged" Expression = { $_.LastWriteTime.ToString("yyyy-MM-dd") } } | Format-Table -AutoSize | Out-String | Write-Host $Choice = Read-Host "(D)elete empty folders, or (A)bort?" if ($Choice -ine "D") { Write-Host "Aborted by user - nothing was deleted." exit } $DeletedCount = 0 $Errors = 0 foreach ($Folder in $EmptyFolders) { try { Remove-Item -Path $Folder.FullName -Recurse -Force -ErrorAction Stop Write-Host "Deleted: $($Folder.FullName)" $DeletedCount++ } catch { Write-Host "ERROR deleting $($Folder.FullName): $($_.Exception.Message)" $Errors++ } } Write-Host "" Write-Host "========================================" Write-Host "Summary: $DeletedCount folder(s) deleted, $Errors error(s)." Write-Host "========================================"