# Remove-Item and -Recurse

**URL:** <https://forums.powershell.org/t/remove-item-and-recurse/12490>\
**Category:** PowerShell Help\
**Created:** [May 2, 2019, 11:08am UTC](https://forums.powershell.org/t/remove-item-and-recurse/12490 "2019-05-02T11:08:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steviebuk](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@steviebuk](https://forums.powershell.org/u/steviebuk)\
**Post date:** [May 2, 2019, 11:08am UTC](https://forums.powershell.org/t/remove-item-and-recurse/12490/1 "2019-05-02T11:08:44Z")

</div>

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](http://_.name/)}
```

I’m now on folder 2004 having done folder 2003 manually and there are over 20 folders within 2004 ☹ 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 ☹

Hopefully that all makes sense.

&nbsp;

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [May 2, 2019, 11:35am UTC](https://forums.powershell.org/t/remove-item-and-recurse/12490/2 "2019-05-02T11:35:07Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![steviebuk](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@steviebuk](https://forums.powershell.org/u/steviebuk)\
**Post date:** [May 3, 2019, 2:56am UTC](https://forums.powershell.org/t/remove-item-and-recurse/12490/3 "2019-05-03T02:56:29Z")

</div>

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 🙂

&nbsp;

Thank you.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/remove-item-and-recurse/12490/4 "2024-05-16T20:32:51Z")

</div>


