Published by


Kari's Avatar
Finnish but not finished


Location: A Finnish expat in Germany
Posts: 12,949

Show Printable Version 


information   Information
One of many PowerShell security features is that you can't run a script simply by double clicking it's icon on desktop or in a folder. Doing so will by default open script in Notepad for editing instead. You can right click script and select Run with PowerShell, open it in PowerShell and run manually, run it from another script or call PowerShell to run it from Command Prompt or a batch file, but you can't associate .ps1 (PS script extension) to be run when you double click the icon.



This tutorial will show how to work around that limitation by creating a shortcut to run any PS script from shortcut.


 Create a Shortcut to run PowerShell Script


1.) Right click a script, select Create shortcut:
Name:  image.png
Views: 150
Size:  315.6 KB

2.) Right click the shortcut, select Properties:
Name:  image.png
Views: 148
Size:  129.5 KB

3.) The Target field under Shortcut tab shows full path to file this shortcut will open:
Name:  image.png
Views: 141
Size:  133.4 KB

4.) In this example case now, I've made a shortcut for USBUpdate.ps1 script made in this tutorial. As my Users folder is relocated to drive E: and I have the script in folder Users\Kari\OneDrive\PS Scripts, in my case now Target shows following path:

"E:\Users\Kari\OneDrive\PS Scripts\USBUpdate.ps1"

Notice that path must be in quotes because it contains a space.

5.) At the moment shortcut would act exactly as the script icon, open script in Notepad for editing. I need to edit Target to tell system to launch PowerShell and then run the script. I'll add all that in front of path which is already there. New Target looks like this, part added before the actual path shown in blue:

powershell.exe -ExecutionPolicy Bypass -File "E:\Users\Kari\OneDrive\PS Scripts\USBUpdate.ps1"
Name:  image.png
Views: 141
Size:  126.5 KB

Note: the above command can also be used to run script from a batch file or from Run prompt (WIN + R).

First part of command powershell.exe will launch PowerShell. Next, I will set it to bypass any existing execution policy which might prevent my script to run with switch -ExecutionPolicy Bypass, and finally tell with -File switch that I want to run script told in following path.

Instead of bypassing current execution policy you can use Unrestricted, AllSigned or RemoteSigned settings. See more about execution policies and what each of them means: Using the Set-ExecutionPolicy Cmdlet

Click OK to save and close Properties.

6.) If your script needs to be run elevated (Run as administrator) and it does not include function for self-elevation, see this tutorial to make it run elevated: Create Elevated Shortcut without UAC prompt in Windows 10 Customization Tutorials

To make script self-elevate, see how to do it in step 3.3 in following tutorial: PowerShell Scripting - Update Windows 10 USB install media Installation Upgrade Tutorials

7.) I can now copy or move script shortcut to desktop or any other location or pin it to Start or Taskbar:
Name:  image.png
Views: 142
Size:  263.1 KB

When double clicked or selected in Start, the script runs:
Click image for larger version. 

Name:	image.png 
Views:	8 
Size:	426.4 KB 
ID:	161963


That's it, easy and fast!

Kari