简体   繁体   中英

How do I execute a powershell script by a shortcut key?

So I have successfully bound a key to open this script:

快捷方式设置

But the way to run that script with PowerShell is to right-click it and go Open with PowerShell . Using the key binding, it just opens it normally (so with notepad).

How can I make it so it opens with PowerShell from that binding?

You already have a lnk that points to your script file. Make that point to PowerShell and pass your file as a parameter. Assuming your file is located in C:\PSScripts you would have to set the Target of the lnk to

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& C:\PSScripts\Display Off.ps1" -NoLogo -NonInteractive -NoProfile

在此处输入图像描述

So, you can't run PS script, because security feature. User can't double click it. You can create simple.bat like:

powershell -command Set-ExecutionPolicy RemoteSigned 
powershell -command Your Script
powershell -command Set-ExecutionPolicy Restricted

Check too this topic: Is there a way to make a PowerShell script work by double clicking a.ps1 file? There are more solutions there.

Go to the General tab in your shortcut settings, and change the Open with row from notepad to

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM