# Foreach remove-item help

**URL:** https://forums.powershell.org/t/foreach-remove-item-help/3649
**Category:** PowerShell Help
**Created:** [January 21, 2015, 6:15am UTC](https://forums.powershell.org/t/foreach-remove-item-help/3649 "2015-01-21T06:15:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![daniel-brooks](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@daniel-brooks](https://forums.powershell.org/u/daniel-brooks)
#### Post date: [January 21, 2015, 6:15am UTC](https://forums.powershell.org/t/foreach-remove-item-help/3649/1 "2015-01-21T06:15:07Z")

</div>

I’m trying to delete everything on the desktop from a txt file containing computer names(40 pcs - separate domain). Maybe my syntax is off. I tried using invoke-command inside the foreach loop and wasn’t successful either.

$computers = Get-Content C:\mypc.txt  
foreach ($comp in $computers)  
{  
Remove-item -Path “\$comp\c$\Users\desktop\*”  
}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [January 21, 2015, 6:29am UTC](https://forums.powershell.org/t/foreach-remove-item-help/3649/2 "2015-01-21T06:29:55Z")

</div>

Your code is basically okay, assuming that you have appropriate rights on all of the computers you’re connecting to (access to the c$ share, permission to remove files from users’ desktops, etc. Basically, Administrator), with one exception. There is no C:\Users\Desktop folder. Instead, you want to remove the contents of the Desktop folder from each user profile found in C:\Users (assuming that user profile directories are their default values and haven’t been redirected.) Try this:

```
Remove-Item -Path "\\$comp\c$\Users\*\desktop\*" -WhatIf
```

I added the -WhatIf switch because this is a destructive command, and you might want to make sure you like what it’s doing first. You might also want to add a -Recurse switch to the command, if there are folders on these desktops that you want to clear out.

---

<div class="post-metadata">

### Author: ![daniel-brooks](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@daniel-brooks](https://forums.powershell.org/u/daniel-brooks)
#### Post date: [January 21, 2015, 6:37am UTC](https://forums.powershell.org/t/foreach-remove-item-help/3649/3 "2015-01-21T06:37:40Z")

</div>

Thank you! It looks like I was only one \* off. I wish I would have known about that -WhatIf command earlier, that is extremely helpful.

---

<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:46pm UTC](https://forums.powershell.org/t/foreach-remove-item-help/3649/4 "2024-05-16T20:46:29Z")

</div>


