16

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?

| improve this question | | | | |
19

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 }
| improve this answer | | | | |
  • 1
    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
14

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}
| improve this answer | | | | |
  • 1
    This deletes files from subdirectories as well - possibly not what the OP intended... – Robbie Dee Feb 17 '16 at 14:22
  • 1
    This answer should be accepted one. I choose you! – Teoman shipahi Jan 11 '17 at 21:24
4

You were nearly there, you just needed:

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

@bourne almost had it:

Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer } | remove-item
| improve this answer | | | | |
  • 3
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – rnxrx Jan 17 '17 at 21:47
  • What do you mean it doesn't provide an answer? What does it do? It is the only complete answer on the page. – Mordechai Jan 18 '17 at 6:31
  • It's not a self-sufficient answer. To the point in your response it's a correction to another answer and should be included as a comment. – rnxrx Jan 18 '17 at 16:25
  • sorry, I guess you don't understand powershell. Just because I refer to another answer, it doesn't make mine any less "self-sufficient", – Mordechai Jan 18 '17 at 18:05
  • you might want to explain the issue with the other answer, making it a command. Otherwise you might want to explain what your Powershell command does – Dennis Nolte Jan 19 '17 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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