0

I am trying to run a simple command from powershell, but as always with powershell nothing works.

I cannot get this to work regardless how many different quotes I try.

PS> Measure-Command { C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe test.sln /Build } 
mklement0
  • 382,024
  • 64
  • 607
  • 775
Generic Name
  • 1,083
  • 1
  • 12
  • 19

1 Answers1

2

A command lines such as

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

wouldn't work in any shell, because, due to the lack of quoting around the executable path, necessitated by that path containing spaces, would result in C:\Program being interpreted as the executable path. In other words: quoting such a path is a must.

What is PowerShell-specific is the need to use &, the call operator whenever an executable path is quoted and/or contains variable references / expressions (which is a purely syntactical requirement explained in this answer; if you don't want to think about when & is required, you may always use it).

Additionally, as you've discovered yourself, for synchronous, console-based operation, you must call devenv.com, not devenv.exe.[1]

Therefore:

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

Note that since the path a literal one (no variable references / expressions), use of single-quoting ('...') is preferable; for more information about string literals in PowerShell, see the bottom section of this answer.


[1] In case you need to invoke a GUI-subsystem application synchronously (wait for it to exit) - which run asynchronously by default - use Start-Process -Wait.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • This command "Measure-Command { & 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe' test.sln /Build }" has the effect that devenv is started in the background, so Measure-Command measures how long time it takes to start it. Not how long it takes to build the solution. – Generic Name Jun 14 '21 at 07:40
  • Oh man. Turns out I have to use devenv.com. Then it works. – Generic Name Jun 14 '21 at 07:54
  • Good to know, @GenericName - I've updated the answer accordingly. – mklement0 Jun 14 '21 at 12:29