I have a POSH script that I’m trying to run that will simply clear the recycle bin. However, every time I try to run it from a configured shortcut, it just closes immediately, thus failing to run the code.
The script will only run successfully if I:
Open POSH > open script > run
Right-click on script > choose Run with PowerShell.
I have two other scripts that run from a shortcut that’s configured to use PowerShell to run the script in the Target properties field. I’m just not sure why this one is so damn stubborn.
The filename is Clear-RecycleBin.ps1 and contains a single line of code:
Clear-RecycleBin -Force
I’ve created a shortcut to the file using the following properties that will run the script with POSH automatically without having to right-click > Run with PowerShell.
FYI: When clicking Apply or OK, the full path to POSH is automatically added: C:\Windows\System32\WindowsPowerShell\v1.0\
The Advanced Properties for the shortcut file is configured to Run as administrator
My Execution Policy was set to Unrestricted for this testing as well.
Even tried baking it into the shortcut target path: Target:powershell.exe Set-ExecutionPolicy Unrestricted -Force "Clear-RecycleBin.ps1"
I tried the following to get it to pause:
Clear-RecycleBin -Force
Pause
Has anyone seen this before or have any ideas why this script will not run using the Shortcut method? Does this behave the same way on your system? I’m on W10 Pro for Workstations v22H2.
I never had any issues with the command itself. It was just when trying to automate it by simply clicking on a configured file shortcut to run the script. I have two others that worked using this method; the only difference is the code in the scripts themselves.
Yes, but it failed to run using the full file path in the Target field. My other scripts would only work if it was just the filename in the Target field, but the path to the file needed to be in the Start In field to work.
As for the full path to PowerShell, it always autocompleted.
For example, if I only entered powershell.exe "Clear-RecycleBin.ps1" in the Target field > click Apply, Windows would automatically update it with C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "Clear-RecycleBin.ps1"
Solution:
I added your suggested -NoExit -File to the shortcuts Target field and the script ran successfully… considering the Recycle Bin was cleared.
I added pause to the bottom to prevent it from exiting so I could see any errors that may have been occurring.
Unfortunately, this script was not closing because it completed its task. It was ignoring my pause command and didn’t clear my Recycle Bin , so I knew there was an issue with the script.