Writing a Powershell script that deletes all the contents from the recycle bins for virtual servers. For some reason I’m running into an error with finding the path for the Windows 2003 recycle bin and can’t locate bin in order to delete everything in it. Was wondering if anyone here could give me some advice on what I am doing wrong with this code-snippet:
if($serverVersion.name -like '*2003*'){
$dir = "\\$server" + '\C$\recycled'
}
elseif($serverVersion.name -like '*2008*'){
$dir = "\\$server" + '\C$\$recycle.bin'
}
$recycleArray = @()
foreach ($item in get-childitem -path $dir){
$recycleArray += $item
}
for ($i = 0; $i -le $recycleArray.length; $i++){
$removal = $dir + "\" + $recycleArray[$i]
remove-item $removal -force -recurse
}
I’m able to delete everything from the W2K8 recycle bin correctly so the code should work correctly once I’m able to find the path to the recycle bin. Here’s a picture of the error message I receive for those curious in seeing:

Also, out of curiosity, is there a way to cut down all this code and make 2 one-liners for both 2003 and 2008? I realize that the current way I’ve written this out doesn’t take advantage of Powershell’s cmdlets and want to improve on it once I’ve figured out what’s wrong with W2K3 recycle bin.
The problem is that the recycle bin is found on this location in widows server 2003 c:\recycler not c:\recycled so jsut change your code and it should work.
Try this code and see if it fixes the problem