Remove only empty Folder

Hi,

I want to delete Empty Folders with PSAppDeployToolkit.
If any files or sub-folder exist, it needs to be skipped.

Get-ChildItem $tdc -Recurse -Force -Directory | 
    Sort-Object -Property FullName -Descending |
    Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } |
    Remove-Item -Verbose

Ref: windows 8.1 - How to recursively remove all empty folders in PowerShell? - Stack Overflow

1 Like

Get-ChildItem -Path ‘Path to scan’ | Where-Object { (Get-ChildItem -Path $_.FullName) -eq $null } | Remove-Folder

This is what I use:

# Remove folders only if empty
$FoldersToRemove = @("$envProgramFiles\Autodesk\Revit LT 2020\WebServices",
                     "$envProgramFiles\Autodesk\Revit LT 2020\ACADInterop",
                     "$envProgramFiles\Autodesk\Revit LT 2020\AddIns",
                     "$envProgramFiles\Autodesk\Revit LT 2020",
                     "$envProgramFiles\Autodesk")

foreach ($Folder in $FoldersToRemove) {
    if ((Test-Path -Path $Folder -PathType Container) -and (Get-ChildItem -Path $Folder).Count -eq 0) {
        Remove-Folder -Path $Folder
    }
}