PowerShell: List Empty Dir 🚀

By Xah Lee. Date: . Last updated: .

List Empty Directories

function xah-list-empty-dir {
    # .DESCRIPTION
    # list empty dirs in current dir, recursively
    # .NOTES
    # Version: 2022-03-03 2023-08-17
    # .LINK
    # http://xahlee.info/powershell/powershell_list_empty_dir.html
    Get-ChildItem -Directory -Recurse | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
}

Delete Empty Directories

function xah-delete-empty-dirs {
    # .DESCRIPTION
    # delete empty dirs in current dir, recursively
    # .NOTES
    # Version: 2022-12-12 2024-05-03
    # .LINK
    # http://xahlee.info/powershell/powershell_list_empty_dir.html
    $empdirs = Get-ChildItem -Directory -Recurse | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
    Write-Host "Deleting:"
    $empdirs | ForEach-Object { $_.fullname } | Out-Host
    $empdirs | Remove-Item -Confirm
}

PowerShell, Working with Directory