function DeNestFolders { $ErrorActionPreference = "Stop" echo "denesting folders!!" $denests = 0 ls -r -directory | %{ $CurrentPath = $_.FullName $ThisLs = ls -LiteralPath $CurrentPath If($ThisLs.Count -gt 0) { If( ($ThisLs[0].GetType().Name -eq "DirectoryInfo") -and ($ThisLs.Count -eq 1)) { $denests += 1 echo "$($ThisLs[0].FullName) is nested, denesting" cp -LiteralPath $ThisLs[0].FullName "$CurrentPath\..\" -recurse -force rm -LiteralPath $ThisLs[0].FullName -recurse } } } echo "finished denesting, $denests folders denested" $removals = 0 echo "removing empty folders!" ls -r -directory | %{ $CurrentPath = $_.FullName $ThisLs = ls -LiteralPath $CurrentPath if(($ThisLs).Count -eq 0) { $removals += 1 echo "$CurrentPath is empty, removing" rm -LiteralPath $CurrentPath } } echo "finished removing empty folders, $removals folders removed" }