I'm trying to delete all files (but not directories) in D:\MyTemp folder, I've tried:
Remove-Item "D:\MyTemp"
Remove-Item "D:\MyTemp\*"
However, when I check, all the files are still there.
What am I missing?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this communityTry this:
Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer }
Found it here: https://superuser.com/questions/150748/have-powershell-get-childitem-return-files-only
To delete all files in the specified directory only (ignoring sub-dirs):
Remove-Item "D:\MyTemp\*.*" | Where { ! $_.PSIsContainer }
The accepted answer didn't work for me, instead I needed:
Get-Childitem -File | Foreach-Object {Remove-Item $_.FullName}
To include folders as well as files, add -Recurse
:
Get-Childitem -File -Recurse | Foreach-Object {Remove-Item $_.FullName}
@bourne almost had it:
Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer } | remove-item
The simplest way I'm aware of would be the following (obviously navigate to the directory you want to empty files from):
Get-ChildItem -File -Recurse | Remove-Item
I'm not sure if this requires a minimum version but I'm pretty sure this has worked for a long time.