Remove-Item and -Recurse

Struggling with this. My sister has loads of old photos she wants. I backed them up years ago. Have them all and looking through them as my nephews 18th soon and noticed when they originally saved them, for some odd reason they had duplicates of every photo.

So the file names are similar to this:

odds 286 (2).jpg

odds 286.jpg

Having forced myself not to ask for help for the whole Powershell script of “how do I delete all those with (2)” I found what I wanted and it works. But it only works in the folder I’m in. I now need it to work in all the other folders otherwise it will take ages having to switch to the new folder and then run the line of script again.

I’ve been trying -Recurse on the script I have but it’s not working as I don’t believe it’s supported with that bit of script I’m using.

This is the code

get-childitem | Where-Object name -Like '*`(2)*' | ForEach-Object { remove-item -LiteralPath $_.name }

I’m now on folder 2004 having done folder 2003 manually and there are over 20 folders within 2004 :frowning: I can’t CD to every folder and then just run that line, it will take me forever. So as I said above, I’d now like the script to look in the 2004 folder and run itself on every other folder that is in the 2004 folder. I’ve tried putting -Recurse in different locations of the line above when reading Microsofts guide on Remove-Item but it didn’t work :frowning:

Hopefully that all makes sense.

 

Try -fullname to get the full path:

get-childitem -recurse | Where-Object name -Like '*`(2)*' | ForEach-Object { remove-item -LiteralPath $_.fullname -whatif }

Or just (-filter just makes it a little faster):

get-childitem -recurse -filter '*(2)*'  | remove-item -whatif

THAT’S IT!!! Thanks, it worked!

The fullname bit is where I was going wrong.

I was also briefly confused by the -whatif but then remembered reading what that was for earlier yesterday :slight_smile:

 

Thank you.