We have folder redirection setup for My Documents and I have noticed that there are $RECYCLE.BIN showing up in several different locations. I have seen several posts about issues with recycle bin and folder redirection. I have seen them located in My Documents and Favorites, although users won’t always have them in one or both, indeed some users don’t have it at all.

Is there a PowerShell script that can search out all $RECYCLE.BIN instances on a drive and empty them no matter where they are located

I know that the recycle bin can be turned off to delete files straight away, that’s not what I want do right now

I have a rudimentary script:

Get-ChildItem "F:\Home\userabc\My Documents\$RECYCLE.BIN\*" | Remove-Item -recurse

When I run it with or without the ", it fails to find the $RECYCLE.BIN in that location.

I am running PowerShell as system, as per this link too, to see if it’s a rights issue.

7 Spice ups

I would try

Get-ChildItem "F:\Home\userabc\My Documents\$RECYCLE.BIN" | Remove-Item -recurse

You don’t need the “*”, you’re already getting the child items from that folder. I could be wrong, but I think that’s what’s preventing it from working just from a glance.

2 Spice ups

Ah, I realize now that I had an extra “*”- “F:\Home\userabc\My Documents$RECYCLE.BIN__*__”

I ran it without it and still get the below

Cannot find path 'F:\Home\userabc\My Documents\.BIN' because it does not exist

You’ll note it’s barking because the $RECYCLE, is this due to the “$RECYCLE” somehow throwing the script off?

1 Spice up

Ah, yeah, that would do it. You should enclose it in literal quotes (’ - single quotes mean literal)

Get-ChildItem 'F:\Home\userabc\My Documents\$RECYCLE.BIN' | Remove-Item -recurse

The other option is to escape the $ with the ` grave key

Get-ChildItem "F:\Home\userabc\My Documents\`$RECYCLE.BIN" | Remove-Item -recurse

I think one of those may work.

2 Spice ups

So the script no longer errors, but it’s not deleting the files within the $RECYCLE.BIN for the user. Tried with the -force switch too

1 Spice up

Unless you use the -Force parameter I don’t believe Get-ChildItem returns hidden items (which is both the bins and the items within the bins). If you want to empty all of them see if this works:

$RecycleBins = Get-ChildItem -Path 'D:' -Filter '$RECYCLE.BIN' -Recurse -Force | Where {$_.PSIsContainer}
ForEach ($Bin in $RecycleBins) {Get-ChildItem -Path $Bin.FullName -Force | Remove-Item -Force -Recurse}
2 Spice ups

If I want to go with small steps first, instead of whole hog, will the below work for one user? Taking into account, I only want to delete from the $RECYCLE.BIN and nothing else

$RecycleBins = Get-ChildItem -Path 'F:\Home\userabc\My Documents\' -Filter '$RECYCLE.BIN' -Recurse -Force | Where {$_.PSIsContainer}
ForEach ($Bin in $RecycleBins) {Get-ChildItem -Path $Bin.FullName -Force | Remove-Item -Force -Recurse}

You must run it in the user’s context.

Yeah that should work fine - The first line is only looking for subfolders that literally have the name ‘$RECYCLE.BIN’, so unless your users have decided to create some subfolders with that name too it wont remove anything else. Just make sure to only use single quotes as Jrx said

I was running with -whatif to test