代码之家  ›  专栏  ›  技术社区  ›  Paul Hollingsworth

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

  •  8
  • Paul Hollingsworth  · 技术社区  · 16 年前

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

    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 回复  |  直到 6 年前
        1
  •  11
  •   Peter Mortensen icecrime    6 年前

    使用此:

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

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

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

    或者使用别名:

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

    使用invoke项意味着将使用正确的Windows文件处理程序。所以对于一个exe文件,它会运行它。例如,对于.doc文件,它将在Microsoft Word中打开它。

    这是最简单的PowerShell命令行之一。试一试:

    ii .
    
        2
  •  19
  •   Dan Solovay    8 年前

    试着在命令前加上一个和号。例如

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