Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

Try this:

Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer }

Found it here: http://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 }
share|improve this answer
    
This only lists file in the current directory –  SteB Nov 2 '12 at 12:15
    
My bad, wasn't sure if you wanted to do recurse. I'll edit the original. You can just add -recurse to the Get-ChildItem command –  bourne Nov 2 '12 at 12:17
    
This works (only delete files from specified directory, ignoring sub-dirs): Remove-Item "D:\MyTemp\*.*" | Where { ! $_.PSIsContainer } –  SteB Nov 2 '12 at 12:22
    
That's great. Glad you got it working. –  bourne Nov 2 '12 at 12:42
    
Sorry btw SteB, I just noticed I forgot to include the Remove-Item. It's going to be a long day wow! –  bourne Nov 2 '12 at 12:50
add comment

The accepted answer didn't work for me, instead I needed:

Get-Childitem -File -Recurse | Foreach-Object {Remove-Item $_.FullName}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.