Reputation: 3797
I have a folder whose entire entire contents I want to delete, but I want to keep the actual folder. I have tried this:
function deleteFiles {
# The parameter.
param([string]$sourceDir)
# Move the files
Get-ChildItem -Path $sourceDir -Include *.* -Recurse | foreach { $_.Delete()}
#Delete empty directories
Get-ChildItem -Path $sourceDir -recurse | Where-Object {
$_.PSIsContainer -eq $true -and (Get-ChildItem -Path $_.FullName) -eq $null
} | Remove-Item
}
However as one of the subdirectories has its own sub directories they aren't deleted.
Upvotes: 7
Views: 16251
Reputation: 200293
This should suffice:
Remove-Item -Path "$sourceDir\*" -Recurse
Upvotes: 16