September 19, 2019

Using MSBuild with PowerShell

Using Visual Studio's MSBuild from PowerShell scripts.

If you are building software on Windows, you may need to script execution of the build. PowerShell is natively available on modern Windows systems and provides a powerful and flexible scripting solution.

This script snippet can be used to locate a Visual Studio 2017 installation with MSBuild. It will take care of loading the Visual Studio environment variables, after which you can easily invoke MSBuild or another tool that will use MSBuild (like CMake).

You may need to add additional "-requires" arguments to restrict the instances of Visual Studio to those that have the required components installed. You can update the "-version" argument if you need to locate a different version of Visual Studio.

As a bonus, this snippet can be used to find CMake and add it to the PATH environment variable: