1

If I RDP to my Windows 2016 server (called LAUREL) and run this powershell, it works fine:

Clear-RecycleBin -DriveLetter C -force

However if I run from my Windows 10 workstation logged on as a Domain Admin, the command:

icm -ComputerName laurel -ScriptBlock {Clear-RecycleBin -DriveLetter C -force}

I get:

The system cannot find the path specified At line:1 char:1 + icm -ComputerName laurel -ScriptBlock {Clear-RecycleBin -DriveLetter ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception + FullyQualifiedErrorId : FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand + PSComputerName : laurel

Any ideas on how to clear a recycle bin on a remote server? This snippet of powershell will be integrated into an automated VM build.

Mark Allison
  • 2,188
  • 7
  • 26
  • 45
  • [This script](https://gallery.technet.microsoft.com/scriptcenter/Get-RecycleBinSize-092f15c7) maybe helpful. – LotPings Dec 23 '16 at 20:18
  • Isn't the recycle bin per-user rather than per-computer? When invoking remotely, whose recycle bin should get cleared? – Bill_Stewart Dec 29 '16 at 16:50
  • @Bill_Stewart The account which has the security context of the remote connection. – Mark Allison Dec 29 '16 at 17:43
  • Have you verified that's actually the case? (Unfortunately, wishful thinking doesn't cause features to spring into existence.) Just food for thought. – Bill_Stewart Dec 29 '16 at 17:47
  • Yes with `icm -ComputerName laurel -ScriptBlock { gci env:username }` – Mark Allison Dec 29 '16 at 19:33
  • That doesn't necessarily mean that the `Clear-RecycleBin` cmdlet will work likewise (hence your question). – Bill_Stewart Dec 30 '16 at 04:36
  • I can repro this, but don't see anything super obvious in the underlying code. Interestingly, if you remove the -Force, you'll get a prompt to confirm the action, and no error after you confirm. Not sure if it actually still runs the empty or not, but it's definitely weird. – Matthew Wetmore Jan 01 '17 at 22:36
  • Further, it looks like the recycle bin actually DOES empty, despite the error. – Matthew Wetmore Jan 01 '17 at 22:44

2 Answers2

2

For the moment, I'd suggest ignoring the error, using -ErrorAction SilentlyContinue

icm -ComputerName laurel -ScriptBlock {Clear-RecycleBin -DriveLetter C -force -ErrorAction SilentlyContinue}

Despite the error, at least in my environment, the recycle bin is actually cleared and ignoring the error will let your script continue.

I can repro this, but haven't figured out why yet - but will pass it along to the appropriate team. Interestingly, if you remove the -Force, you'll get a prompt to confirm the action, and no error after you confirm.

Matthew Wetmore
  • 1,633
  • 12
  • 21
0

I found an issue closed which explains it is a bug in ClearRecycleBinCommand.cs

The bug finder recommends to use Clear-RecycleBin -ErrorAction SilentlyContinue to prevent irrelevant ErrorRecord.

This bug fixed in PowerShell v7.0.0-preview.6

zichen.L
  • 101
  • 1