2

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,278

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.

1

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
  • 111