17

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?

SteB
  • 989
  • 6
  • 15
  • 31

5 Answers5

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 }
bourne
  • 314
  • 2
  • 7
  • 1
    This only lists file in the current directory – SteB Nov 02 '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 02 '12 at 12:17
  • This works (only delete files from specified directory, ignoring sub-dirs): Remove-Item "D:\MyTemp\\*.*" | Where { ! $_.PSIsContainer } – SteB Nov 02 '12 at 12:22
  • That's great. Glad you got it working. – bourne Nov 02 '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 02 '12 at 12:50
  • NB: running `update-help` then `help remove-item -examples` you'll see that `remove-item`'s `-recurse` parameter is faulty; hence the need to use `get-childitem` to take advantage of that parameter. – JohnLBevan Nov 07 '14 at 18:27
15

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}
79E09796
  • 254
  • 2
  • 6
4

You were nearly there, you just needed:

Remove-Item "D:\MyTemp\*.*"
Robbie Dee
  • 141
  • 3
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.

Zac
  • 11
  • 1
1

@bourne almost had it:

Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer } | remove-item
Mordechai
  • 111
  • 3
  • 3
    This does not provide an answer to the question. Once you have sufficient [reputation](http://serverfault.com/help/whats-reputation) you will be able to [comment on any post](http://serverfault.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/304310) – 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 06: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
  • this is a lot of back and forth over nothing. I was just trying to be helpful. @Dennis, your comment isn't comprehensible, so I can't answer it. If this site isn't interested in helpful answers, I apologize and will go to a "read only" mode. – Mordechai Jan 19 '17 at 14:44