PowerShell script keeps closing without running code


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:

  1. Open POSH > open script > run
  2. 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.

Target: powershell.exe "Clear-RecycleBin.ps1"
Start in: D:\Scripts\File-System

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

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.

Thanks in advance for any help.

You know PowerShell.exe has a help, don’t you? Have you read it?

powershell /?

Have you tried providing the full path of both - the PowerShell executable AND the script file?

I’d tried to run a script like this if I’m unsure about it:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -File '<Full Path To Your Script File>\Clear-RecycleBin.ps1'
1 Like

It is for the security of the PowerShell to keeps closing script without running code.

Hi Olaf,

Thanks for getting back to me on this.

Yes, I was using Microsoft’s example to Clear all recycle bins without confirmation using Clear-RecycleBin -Force from their online help page: Clear-RecycleBin (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn

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"


I added your suggested -NoExit -File to the shortcuts Target field and the script ran successfully… considering the Recycle Bin was cleared.

This was very helpful!

Again, thank you for all your help, Olaf.

True, but it depends on how the script is ran.

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.

Thanks for your feedback…