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

时间:2009-01-14 15:32:35

标签: powershell

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

PS C:\> echo Hello World
Hello
World
PS C:\> echo 'Hello World'
Hello World
PS C:\> echo "Hello World"
Hello World

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

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:\>

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

2 个答案:

答案 0 :(得分:19)

尝试在命令前加上&符号。例如

& 'C:\Program Files\winscp\winscp.exe'

答案 1 :(得分:11)

使用此:

. "c:\program files\test.exe"

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

Invoke-Item "c:\program files\test.exe"

或使用别名:

ii "c:\program files\test.exe"

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

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

ii .