PowerShell Scripting - Run a Script from Shortcut  

    PowerShell Scripting - Run a Script from Shortcut

    PowerShell Scripting - Run a Script from Shortcut

    How to run PowerShell scripts from a Shorcut
    Published by Category: General Tips
    07 Nov 2017
    Designer Media Ltd

    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:
    PowerShell Scripting - Run a Script from Shortcut-image.png

    2.) Right click the shortcut, select Properties:
    PowerShell Scripting - Run a Script from Shortcut-image.png

    3.) The Target field under Shortcut tab shows full path to file this shortcut will open:
    PowerShell Scripting - Run a Script from Shortcut-image.png

    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"
    PowerShell Scripting - Run a Script from Shortcut-image.png

    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:
    PowerShell Scripting - Run a Script from Shortcut-image.png

    When double clicked or selected in Start, the script runs:
    PowerShell Scripting - Run a Script from Shortcut-image.png


    That's it, easy and fast!

    Kari



  1. Posts : 10
    Win.8 & Win.10
       #1

    This is a great workaround to start files by double clicking. Thank you.

    I have been using this but it is very annoying that hte Open File Location functionality goes to the PowerShell.exe file instead of the script location. I know you can copy the location from the properties dialog but it is super annoying selecting just what you want within that text. Do you have a better workaround?
      My Computer


  2. Posts : 12
    Windows 10 Pro for Workstations v21H2
       #2

    Just one thing to add... the short version:

    By default, the full path to the file is in the Target field. In my case, to get it to run automatically, I had to remove the file path from the Target field. The Start in field already has the path to the script, so PowerShell knows where to find the script.

    Failed: (with file path in Target)
    Target: powershell.exe -File "D:\Scripts\Backup-Restore VMs.ps1"

    Successful: (without file path in Target)
    Target: powershell.exe -File "Backup-Restore VMs.ps1"

    FYI:
    When looking at the properties again, you will see the full path to PowerShell was added automatically after running the script.
    Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "Backup-Restore VMs.ps1"

    Note:
    I excluded the Execution Policy portion because my test system (a VM) is already set to Unrestricted. However, on my host system, I will be using the -ExecutionPolicy Bypass option. Although I haven't' tested that yet, I am confident it will work.


    *Running W10 22H2 in case you're wondering.
      My Computer


  3. Posts : 767
    Windows 7
       #3

    Having the explicit -ExecutionPolicy Bypass is redundant in your case, but it's important to show on every tutorial -- since we don't know the user's current policy. -ep bypass is a shorter alias; same with -nop for -NoProfile.
      My Computer


 

Tutorial Categories

PowerShell Scripting - Run a Script from Shortcut Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:29.
Find Us




Windows 10 Forums