如何在PowerShell上转义命令行参数?

Pau*_*rth 9 powershell

看来我可以使用单引号或双引号来转义命令行参数:

PS C:\> echo Hello World
Hello
World
PS C:\> echo 'Hello World'
Hello World
PS C:\> echo "Hello World"
Hello World
Run Code Online (Sandbox Code Playgroud)

但是仍然有一些我无法弄清楚的东西,当你希望从包含空格的目录中运行可执行文件时:

PS C:\> c:\program files\test.exe
The term 'c:\program' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:11
+ c:\program  <<<< files\test.exe
PS C:\> 'c:\program files\test.exe'
c:\program files\test.exe
PS C:\> "c:\program files\test.exe"
c:\program files\test.exe
PS C:\>
Run Code Online (Sandbox Code Playgroud)

如何让PowerShell运行上面的可执行文件?

Rad*_*Rad 19

尝试在命令之前放置一个&符号.例如

& 'C:\Program Files\winscp\winscp.exe'
Run Code Online (Sandbox Code Playgroud)


EBG*_*een 11

用这个:

. "c:\program files\test.exe"
Run Code Online (Sandbox Code Playgroud)

实际上更好的解决方案是:

Invoke-Item "c:\program files\test.exe"
Run Code Online (Sandbox Code Playgroud)

或使用别名:

ii "c:\program files\test.exe"
Run Code Online (Sandbox Code Playgroud)

使用Invoke-Item意味着将使用正确的Windows文件处理程序.因此,对于EXE文件,它将运行它.例如,对于.doc文件,它将在Microsoft Word中打开它.

这是最方便的PowerShell命令行之一.试试看:

ii .
Run Code Online (Sandbox Code Playgroud)