Question:
I have a folder whose entire entire contents I want to delete, but I want to keep the actual folder. I have tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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.
Answer:
This should suffice:
1 2 |
Remove-Item -Path "$sourceDir\*" -Recurse |