Powershell delete files only from directory
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?
Try 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}
You were nearly there, you just needed:
Remove-Item "D:\MyTemp\*.*"