19

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.

27

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.

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.

  • This is helpful for doing a copy/paste of multiple files from Git. – Shaun Luttin Aug 26 at 16:15
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

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.