我如何 运行 来自 powershell 的程序

How do I run a program from powershell

我正在尝试 运行 来自 powershell 的简单命令,但与往常一样,使用 powershell 没有任何效果。

无论我尝试多少种不同的引述,我都无法让它发挥作用。

PS> Measure-Command { C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\devenv.exe test.sln /Build } 

一个命令行如

C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\devenv.exe test.sln /Build

any shell 中不起作用,因为,由于缺少围绕可执行文件路径的引号, 路径包含spaces,将导致 C:\Program 被解释为可执行路径。 也就是说:引用这样的路径是必须的.

什么 PowerShell-specific是需要用到&, call operator whenever an executable path is quoted and/or contains variable references / expressions (which is a purely syntactical requirement explained in ;如果你不想考虑什么时候需要&,你可以总是使用它。

此外,正如您自己发现的那样,对于基于控制台的同步操作,您必须调用devenv.com,而不是devenv.exe[1]

因此:

Measure-Command { 
 & 'C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\devenv.com' test.sln /Build 
} 

请注意,由于路径是 literal 一个(没有变量引用/表达式),所以使用 single-quoting ('...') 更可取;有关 PowerShell 中字符串文字的详细信息,请参阅 this answer.

的底部部分

[1] 如果您需要同步调用 GUI-子系统应用程序(等待它退出)- 运行 asynchronously 默认 - 使用 Start-Process -Wait.