04 December, 2025

PowerShell Batch Script: Delete empty subfolders

Only list deleted folders - no actual deletion:

# List empty folders from the current location which could be recursively deleted

Get-ChildItem -Directory -Recurse | Sort-Object FullName -Descending | Where-Object {
   -not (Get-ChildItem -Path $_.FullName -File -Recurse -ErrorAction SilentlyContinue)
} | Select-Object FullName

Script to recursively delete empty sub-directories:

# Recursively delete all empty directories from the current location

Get-ChildItem -Directory -Recurse | Sort-Object FullName -Descending | ForEach-Object {
    if (-not (Get-ChildItem -Path $_.FullName -File -Recurse -ErrorAction SilentlyContinue)){
         Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
    }
}