18

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?

5 Answers 5

20

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 }
6
  • 1
    This only lists file in the current directory
    – SteB
    Nov 2, 2012 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, 2012 at 12:17
  • This works (only delete files from specified directory, ignoring sub-dirs): Remove-Item "D:\MyTemp\*.*" | Where { ! $_.PSIsContainer }
    – SteB
    Nov 2, 2012 at 12:22
  • That's great. Glad you got it working.
    – bourne
    Nov 2, 2012 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, 2012 at 12:50
16

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

You were nearly there, you just needed:

Remove-Item "D:\MyTemp\*.*"
1

@bourne almost had it:

Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer } | remove-item
6
  • 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, 2017 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, 2017 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, 2017 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, 2017 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 Jan 19, 2017 at 12:42
1

The simplest way I'm aware of would be the following (obviously navigate to the directory you want to empty files from):

Get-ChildItem -File -Recurse | Remove-Item

I'm not sure if this requires a minimum version but I'm pretty sure this has worked for a long time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .