Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 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

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}
share|improve this answer
    
This deletes files from subdirectories as well - possibly not what the OP intended... – Robbie Dee Feb 17 at 14:22

You were nearly there, you just needed:

Remove-Item "D:\MyTemp\*.*"
share|improve this answer

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.