Windows 10: PowerShell Scripting - Run a Script from Shortcut

Discus and support PowerShell Scripting - Run a Script from Shortcut in Windows 10 Tutorials to solve the problem; How to: PowerShell Scripting - Run a Script from Shortcut [img] Information One of many PowerShell security features is that you can't run a script... Discussion in 'Windows 10 Tutorials' started by Brink, May 24, 2016.

  1. Brink
    Brink New Member

    PowerShell Scripting - Run a Script from Shortcut


    How to: PowerShell Scripting - Run a Script from Shortcut

    PowerShell Scripting - Run a Script from Shortcut [​IMG]
    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.


    PowerShell Scripting - Run a Script from Shortcut [​IMG]


    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 [​IMG]


    2.) Right click the shortcut, select Properties:

    PowerShell Scripting - Run a Script from Shortcut [​IMG]


    3.) The Target field under Shortcut tab shows full path to file this shortcut will open:

    PowerShell Scripting - Run a Script from Shortcut [​IMG]


    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 [​IMG]


    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 [​IMG]


    When double clicked or selected in Start, the script runs:

    PowerShell Scripting - Run a Script from Shortcut [​IMG]


    That's it, easy and fast!

    Kari

    :)
     
    Brink, May 24, 2016
    #1
  2. pepanee Win User

    Run PowerShell Script As An Administrator

    Hi. So I've been searching online on a way to do this yet I cannot find a way to do it.

    _______________________________________

    I need this in specific:

    I want to double click a PowerShell script file (which will run as an administrator) from Windows Explorer that has this as the script:
    Set-MpPreference -DisableRealtimeMonitoring $true

    (that script requires that PowerShell needs to be run as an administrator)
    _______________________________________

    So those are the restrictions:
    No right-click>run PowerShell as an administrator
    No opening a shortcut to PowerShell (as an administrator) and pasting the command


    Thanks
     
    pepanee, Oct 26, 2019
    #2
  3. Try3 Win User
    Run PowerShell Script As An Administrator

    Of course, there is always the elevating shortcut method. This requires one extra step compared to setting up such a shortcut for a batch file.

    1 Create a shortcut to your PS1 file [by, for example, dragging it with the Alt key pressed]
    2 Right click on the shortcut & select Properties
    2.1 At the start of the target field, type in powershell.exe then a space to separate it from the script's path-name
    2.2 Click on the Advanced button & set the Run as Admin checkbox.
    2.3 OK, OK
    {you will notice the shortcut's icon change when you do this}

    You can now double-click on the shortcut instead of your original script and it will run as Admin [after the normal Admin challenge].

    The extra step compared to doing this for batch files is 2.1. Batch files can be set to run as Admin without having to job the Target field.

    Denis
     
  4. EckiS Win User

    PowerShell Scripting - Run a Script from Shortcut

    PowerShell script run - Cmdlets

    As SQL-IP and Cosmos are still internal to Microsoft, I wonder why you are not simply contacting the person who provided you with the scripts.

    For point 1: see PowerShell: Bypass ExecutionPolicy to run downloaded scripts

    For point2: this is specific to the script: "Cosmos.Document" is probably in a module which is not found.
     
    EckiS, Oct 26, 2019
    #4
  5. PowerShell script run - Cmdlets

    Greetings - in the last month my role (support for Microsoft) has begun running database searches using PowerShell ISE - the procedure and software I was given by Microsoft to follow specified PowerShell ISE, not PowerShell. The first week or so the searches
    seemed to run fine and I got good results returned. Then the searches stopped successfully executing. I got various error messages, or just illogical results, and so I tried reinstalling PowerShell a couple of times, with no success. On Friday 8/12 I upgraded
    to Win10 Anniversary Ed. I still can't successfully run a PowerShell ISE search request. So, I'm hoping the Forum can help:

    1. When I Run a request I get this pop-up: "Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. If you trust this script, use the Unbock-File cmdlet to allow the script to run without
    this warning message. Do you want to run ...". I then get 3 options 1) Do not run, 2) Run Once, 3) Suspend.

    My understanding is that the "Unbock-File cmdlet" is a single-feature command that manipulates objects in Windows PowerShell. Where should I insert the "Unbock-File cmdlet" ? I trust the script because it was provided to me by Microsoft.

    2. I've been selecting option 2) Run Once. The script will then run for a while, and then I get this error message "[Expression Error] The import Cosmos.Document matches no exports. Did you miss a module reference?"

    How do resolve this issue? Reinstalling PowerShell has not seemed to help.

    Any help much appreciated!
     
    Jacqui Schultz, Oct 26, 2019
    #5
  6. Andre Da Costa, Oct 26, 2019
    #6
Thema:

PowerShell Scripting - Run a Script from Shortcut

Loading...
  1. PowerShell Scripting - Run a Script from Shortcut - Similar Threads - PowerShell Scripting Run

  2. Cannot get PowerShell script to run

    in Windows 10 Software and Apps
    Cannot get PowerShell script to run: Hi,No matter what I try I cannot get permission to run a .ps1 file. I always get this error:& : File C:\Program Files\putty\pagentpass.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at...
  3. Run desktop script in PowerShell

    in Windows 10 Network and Sharing
    Run desktop script in PowerShell: I have a script on my desktop that I need to run in PowerShell; how do I do that? https://answers.microsoft.com/en-us/windows/forum/all/run-desktop-script-in-powershell/666a7726-8cbd-42c7-ac2c-419229432252
  4. Run desktop script in PowerShell

    in Windows 10 Gaming
    Run desktop script in PowerShell: I have a script on my desktop that I need to run in PowerShell; how do I do that? https://answers.microsoft.com/en-us/windows/forum/all/run-desktop-script-in-powershell/666a7726-8cbd-42c7-ac2c-419229432252
  5. Run desktop script in PowerShell

    in Windows 10 Software and Apps
    Run desktop script in PowerShell: I have a script on my desktop that I need to run in PowerShell; how do I do that? https://answers.microsoft.com/en-us/windows/forum/all/run-desktop-script-in-powershell/666a7726-8cbd-42c7-ac2c-419229432252
  6. PowerShell script

    in Windows 10 Support
    PowerShell script: The code below tests a series of IPs in a txt file.The result is as follows and is written in a txt file. 19.191.12.161 - online 19.191.12.162 - offline 19.191.12.163 - offline 19.191.12.164 - offline 19.191.12.165 - offline also see the scrennshot Code: $Output= @()$GetIP =...
  7. powershell scripts

    in AntiVirus, Firewalls and System Security
    powershell scripts: Hi, I have azure. Someone is somehow installing powershell scripts onto my computer that allows them to host my PC through P2P and UDP connections, well I think so anyway. I have only just found powershell script entry changes in my registry by accident. These criminals...
  8. powershell script

    in Windows 10 Customization
    powershell script: looking for a script to monitor for a folder, if there is a file in that folder send it as an email, once the email is sent out, confirm and then move the files to an archive folder. monitor c:\test\*.pdf No files, do not send email. Yes Files, send an email to someone at...
  9. Running PowerShell script as a batch file

    in Windows 10 Network and Sharing
    Running PowerShell script as a batch file: Can I run the Power Shell line: "C:\Windows\System32>get-appxpackage *3dbuilder* | remove-appxpackage" from a .Bat file? ***Original title: Removing bloatware***...
  10. Run PowerShell Script As An Administrator

    in Windows 10 Support
    Run PowerShell Script As An Administrator: Hi. So I've been searching online on a way to do this yet I cannot find a way to do it. _______________________________________ I need this in specific: I want to double click a PowerShell script file (which will run as an administrator) from Windows Explorer that has...