Skip to content

Find MSBuild

Heath Stewart edited this page Jun 1, 2021 · 11 revisions

MSBuild is an optional component for Visual Studio and also installed with Build Tools. By default, vswhere will look for Community, Professional, and Enterprise editions of Visual Studio but you can optionally pass a list of products to search.

The following examples use the latest release to find MSBuild in Community, Professional, and Enterprise. If you want to also support Build Tools, pass -products * on the command line as well. For examples that work with older releases, please view the history of this page.

With Visual Studio 2017 Update 2 or newer installed, you can find vswhere at %ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe, or to make sure it's always available in your repo see Installing for an option using NuGet.

Starting in Visual Studio 2019 Preview, MSBuild will use "Current" instead of the major version number to make it easier to invoke for different versions. Starting with our latest release, you can find either version with a single command. If you want to also support prerelease versions, pass -prerelease to the vswhere command as well.

Batch

Note below that the examples are written as if in a batch script, which requires escaping "%" with another "%" which is why you see "%%i". If you were typing this in the command prompt you would use only one "%" like "%i".

@echo off
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%i in (`vswhere -latest -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe`) do (
  "%%i" %*
  exit /b !errorlevel!
)

PowerShell

The following examples are equivalent to those above but written for PowerShell.

$path = vswhere -latest -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe | select-object -first 1
if ($path) {
  & $path $args
}

The asterisk indicates to search all products. It is not, however, a wildcard for pattern matching.