如何在带有空格的路径中运行PowerShell脚本?

时间:2019-12-20 09:45:43

标签: windows powershell

我在C:\Path with spaces\Script.ps1有一个PowerShell脚本文件。

当我尝试用鼠标双击Windows PowerShell浏览器中的PowerShell控制台打开它时,由于脚本文件本身路径中的空格而引发异常,并且控制台立即关闭。

PowerShell Exception

一种解决方法是用文件参数C:\Path with spaces\Run script.lnk创建PowerShell -File "C:\Path with spaces\Script.ps1"的快捷方式,然后打开它。

我想知道的是,是否有一种方法允许脚本自己在包含空格的路径上运行而无需其他文件。

1 个答案:

答案 0 :(得分:2)

在我的PC上尝试过。使用Powershell可以运行,但是当我使用open with并选择“ C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe”时,它将无法运行。

我能够通过修改注册表来解决它。快速的Google搜索将我带到该键

"HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command"

我复制了值数据

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

并替换了

中的命令
"HKEY_CURRENT_USER\Software\Classes\Applications\powershell.exe\shell\open\command"

我还没有重新启动,也不知道将来的更新可能会如何影响这一点。如果您也有一个以上的用户,这也是当前用户密钥,而不是本地计算机。

请注意,PowerShell的一项安全功能是用户无法双击启动脚本。在此命令“ Set-ExecutionPolicy -Scope Process Bypass”中进行记录。 如果您修改此设置,请格外小心。

在更改之前,我需要提醒您备份注册表项