The other day I showed how to find out how much space was being used by the recycle bin. To clear the folder you can use an expression that takes the result of Get-Child-item and pipes it to Remove-Item. Here’s an example:
Get-ChildItem e:\temp\testme -recurse | remove-item -recurse -whatif
I’m using the -WhatIf parameter so verify what would happen. This is a good habit when performing destructive operations. You may need to use -force to overcome any restrictions like a read-only file. You still need permission to delete the file. Using -force won’t change any permissions.
Because the Recycle folder is hidden, you’ll want to use an expression like this:
Get-ChildItem c:\recycler -force -recurse | remove-item -recurse -force -whatif
This will show what will be deleted. To carry out the dirty deed:
Get-ChildItem c:\recycler -force -recurse | remove-item -recurse -force
You might see an error message about failing to find part of a path, but you should be able to ignore this message. I strongly encourage you to test all of this on a non-production system so that you thoroughly understand how these cmdlets and expressions work.
Hi,
It doesn’t run with Vista. The name’s folder is "$Recycle.bin". Get-childitem didn’t find it.
Any clue?
Cheers,
Sat
Because the directory name in Vista has a $ sign, you have to tell PowerShell not to treat it as a variable. Remember, variables start with a $ in PowerShell. So use the backtick ` to escape the $ sign:
dir `$recycle.bin -rec -force | del -rec -force
Use -Whatif with the DEL or Remove-Item to see what would happen.
If you have other followups on this topic, please post something in the PowerShell forum at ScriptingAnwers.com.
Jeff