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

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

  •  10
  • Paul Hollingsworth  · 技术社区  · 17 年前

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

    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 回复  |  直到 7 年前
        1
  •  11
  •   Peter Mortensen Pieter Jan Bonestroo    7 年前

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

    & 'C:\Program Files\winscp\winscp.exe'
    
        2
  •  19
  •   Dan Solovay    9 年前

    使用这个:

    . "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 .
    
    推荐文章