從 PowerShell 執行可執行檔不適用於路徑中的空格

從 PowerShell 執行可執行檔不適用於路徑中的空格

從 PowerShell 提示字元中:

\Windows\system32\mspaint.exe

將運行畫圖。所以將

Invoke-Expression -command "\Windows\system32\mspaint.exe"

但如果路徑中有空格,PowerShell 會吐出虛擬數據,例如

Invoke-Expression -command "\install\sub directory\test.bat"

其中抱怨:

The term '\install\sub' is not recognized as the name of a cmdlet, function, script file, or operable program.

我缺什麼?

答案1

根據Technet 上的文章中,將路徑用雙引號引起來是不夠的。

您嘗試使用的路徑必須有一個&(和)在目錄前面,否則它將不起作用。

例如:

Invoke-Expression -command & "\install\sub directory\test.bat"

答案2

最簡單的方法是使用呼叫運算子:

&'String containing the path'

請注意,啟動可執行檔Invoke-Expression實際上是錯誤的 cmdlet,最好是使用Start-Process.

相關內容