Spaces cause split in path with PowerShell

前端 未结 12 834
广开言路
广开言路 2020-12-02 15:24

I\'m having an issue with powershell when invoking an exe at a path containing spaces.

PS C:\\Windows Services> invoke-expression \"C:\\Windows Services\\My

相关标签:
12条回答
  • 2020-12-02 15:47

    This worked for me:

    $scanresults = Invoke-Expression "& 'C:\Program Files (x86)\Nmap\nmap.exe' -vv -sn 192.168.1.1-150 --open"
    
    0 讨论(0)
  • 2020-12-02 15:48

    You can escape the space by using single quotations and a backtick before the space:

    $path = 'C:\Windows Services\MyService.exe'
    $path -replace ' ', '` '
    invoke-expression $path
    
    0 讨论(0)
  • 2020-12-02 15:53

    Try this, simple and without much change:

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

    using single quotations at the beginning and end of the path.

    0 讨论(0)
  • 2020-12-02 15:57

    Would this do what you want?:

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

    Use &, the call operator, to invoke commands whose names or paths are stored in quoted strings and/or are referenced via variables, as in the accepted answer. Invoke-Expression is not only the wrong tool to use in this particular case, it should generally be avoided.

    0 讨论(0)
  • 2020-12-02 15:57
    "&'C:\Windows Services\MyService.exe'" | Invoke-Expression
    

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

    0 讨论(0)
  • 2020-12-02 15:57

    Not sure if someone still needs it... I needed to invoke msbuild in powershell and following worked fine:

    $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
    
    0 讨论(0)
提交回复
热议问题