首页 文章

空间导致PowerShell分离路径

提问于
浏览
27

当在包含空格的路径上调用exe时,我遇到了PowerShell的问题 .

PS C:\Windows Services> invoke-expression "C:\Windows Services\MyService.exe"

术语“C:\ Windows”未被识别为cmdlet,函数,脚本文件或可操作程序的名称 . 检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试 .

它似乎在“Windows”和“服务”之间的空间上分裂 . 知道怎么解决这个问题吗?

7 回答

  • 3

    这会做你想要的吗?:

    & "C:\Windows Services\MyService.exe"
    
  • 39

    您可以在空格前使用单引号和反引号来逃避空间:

    $path = 'C:\Windows Services\MyService.exe'
    $path -replace ' ', '` '
    invoke-expression $path
    
  • 8
    "&'C:\Windows Services\MyService.exe'" | Invoke-Expression
    

    通过https://www.vistax64.com/powershell/52905-invoke-expression-exe-has-spaces-its-path.html

  • 0

    不确定是否有人仍然需要它...我需要在powershell中调用msbuild并且以下工作正常:

    $MSBuild = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"
    
    & $MSBuild $PathToSolution /p:OutDir=$OutDirVar /t:Rebuild /p:Configuration=Release
    
  • 5

    因为Invoke-Expression适合我,所以我使用过hack .

    您可以将当前位置设置为包含空格的路径,调用表达式,返回到先前的位置并继续:

    $currLocation = Get-Location
    Set-Location = "C:\Windows Services\"
    Invoke-Expression ".\MyService.exe"
    Set-Location $currLocation
    

    这只有在exe名称中没有空格时才有效 .

    希望这可以帮助

  • 14

    在2018年在Windows10上使用Powershell,对我有用的只是 to replace double quotes " by simple quotes ' . 如答案所示,在空格之前添加反引号打破了路径 .

  • 0

    对于任何带空格的文件路径,只需将它们放在双引号中即可在Windows Powershell中使用 . 例如,如果要转到Program Files目录,而不是使用

    PS C:\> cd Program Files
    

    这将导致错误,只需使用以下将解决问题:

    PS C:\> cd "Program Files"
    

相关问题