只需要以管理员身份运行 CMD 命令行,执行一次以下代码,以后即可双击运行 .ps1 脚本:

命令:

ftype Microsoft.PowerShellScript.1="%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" "%1"

恢复命令:

ftype Microsoft.PowerShellScript.1="%SystemRoot%\system32\notepad.exe" "%1"

若双击出现PowerShell窗口闪退的情况,可能是没有打开Powershell脚本执行的组策略权限,关于Powershell组策略权限的详细解释见Microsoft官方对于PowerShell执行策略的说明,若页面访问出错或找不到页面,尝试在Powershell中运行已编写好的脚本,会给出参阅链接。

一般情况使用如下命令设置当前用户的remote权限即可

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

若要关闭权限,重新设置为Undefined即可

Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser