Delete multiple files on PowerShell command line

19

2

With PowerShell, what is the most concise way to delete multiple explicitly named files?

E.g. on *ix it would be:

rm subDir/a.png anotherDir/b.jpg thirdDir/c.gif

I'm currently using:

echo subDir/a.png anotherDir/b.jpg thirdDir/c.gif|rm

But I consider it suboptimal, so I would like to see alternatives.

Matthew Flaschen

Posted 2011-12-16T23:57:40.837

Reputation: 2 370

Answers

28

You can give PowerShell's rm cmdlet (which is itself an alias for Remove-Item) several files, but you need to separate them with commas.

rm .\subDir\a.png, .\anotherDir\b.jpg, .\thirdDir\c.gif

Check out Get-Help Remove-Item for more details. Or read some documentation on Microsoft's website.

William Jackson

Posted 2011-12-16T23:57:40.837

Reputation: 7 646

4

This is what I ended up using:

echo subDir/a.png anotherDir/b.jpg thirdDir/c.gif|rm

This uses echo to pass three string arguments to rm (Remove-Item). I believe this implicitly uses Remove-Item's -Path parameter. The documentation notes that "The parameter name ("-Path") is optional" and it accepts pipeline input by value.

Matthew Flaschen

Posted 2011-12-16T23:57:40.837

Reputation: 2 370

This is helpful for doing a copy/paste of multiple files from Git. – Shaun Luttin – 2019-08-26T16:15:35.970

0

Old dog's trick, define an array first. Put your stuff in it, and RM the heck out of it.

$myArray = @("subDir/a.png","subDir/b.png","thirdDir/c.gif")
rm $myArray

JP Lizotte

Posted 2011-12-16T23:57:40.837

Reputation: 1