如何从命令行在路径中运行带有空格的Powershell脚本?

问题描述:

因此,我尝试了多种方法从命令行运行Powershell脚本,每个方法都会返回一个错误。

So I've tried a bunch of different ways to run a powershell script from the command line and every single one returns an error.

这是路径:

C:\Users\test\Documents\test\line space\PS Script\test.ps1

我已经尝试过这些:

powershell -File '"C:\Users\test\Documents\test\line space\PS Script\test.ps1"'

powershell "& ""C:\Users\test\Documents\test\line space\PS Script\test.ps1"""

Powershell "& 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"

Powershell -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"

我得到所有这些错误:


& :术语 C:\用户\测试\文档\测试\行空间\PS脚本\不被视为cmdlet,函数,脚本文件或可运行程序的名称。名称的拼写,或者是否包含路径,请验证路径是否正确并重试。

& : The term 'C:\Users\test\Documents\test\line space\PS Script\' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

处理-文件'' C:\Users\test\Documents\test\linespace\PS Script\’失败:给定路径的格式不受支持。为-File参数指定有效路径。

Processing -File ''C:\Users\test\Documents\test\line space\PS Script\'' failed: The given path's format is not support ed. Specify a valid path for the -File parameter.

任何帮助将不胜感激!

在您的示例中,您无缘无故地将引号和双引号混合使用。

In your examples, you're mixing quotes and double quoting for no reason.

IF EXIST "C:\Users\test\Documents\test\line space\PS Script\test.ps1" (
  powershell -ExecutionPolicy Unrestricted -File "C:\Users\test\Documents\test\line space\PS Script\test.ps1"
)