Powershell script error

Ok got a simple PS script that is giving errors. The command runs locally fine. The script is…
Import-Module $env:SyncroModule
Clear-RecycleBin -Force
Log-Activity -Message “All Recycle Bins have been emptied” -EventName “Empty Recycle Bin”
The error is…
error> Clear-RecycleBin : The system cannot find the path specified
error> At C:\ProgramData\Syncro\bin\96a1bda5-d09a-4bb6-87c6-6d11678f7b5e.ps1:4 char:1
error> + Clear-RecycleBin -Force
error> + ~~~~~~~~~~~~~~~~~~~~~~~
error> + CategoryInfo : InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception
error> + FullyQualifiedErrorId : FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand
error>

1 Like

I get this exact same error when I try to run “Clear-RecycleBin -Force” as well on my local powershell on my PC. The second run doesn’t throw that error. I can re-open a new powershell window and reproduce this over and over. The second run is clean, the first one errors out (but still clears recycle bin anyhow). This is all outside of syncro. You’ll want to add -ErrorAction SilentlyContinue onto your Clear-RecycleBin and that should fix it.

I just thought I would double back and share the working script, I ended up with. I ended up piping a CMD command as it was more reliable. I created it as a PowerShell script so I can use the Syncro Module to send info and for future growth.

Import-Module $env:SyncroModule
#runs the command in CMD
“rd /s /q %systemdrive%`$RECYCLE.BIN” | cmd
#writes to script output
Write-Host “All Recycle Bins have been emptied”
#writes to syncro Activity log
Log-Activity -Message “All Recycle Bins have been emptied” -EventName “Empty Recycle Bin”