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