Skip to content

Find MSBuild

Heath Stewart edited this page Mar 4, 2017 · 11 revisions

Find MSBuild

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.

Batch

@echo off

set pre=Microsoft.VisualStudio.Product.
set ids=%pre%Community %pre%Professional %pre%Enterprise %pre%BuildTools
for /f "usebackq tokens=1* delims=: " %%i in (`vswhere -latest -products %ids% -requires Microsoft.Component.MSBuild`) do (
  if /i "%%i"=="installationPath" set InstallDir=%%j
)

if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
  "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)

Starting with version 1.0.32, you can simplify this command in scripts to return only the property - like installationPath - you need.

@echo off

set pre=Microsoft.VisualStudio.Product.
set ids=%pre%Community %pre%Professional %pre%Enterprise %pre%BuildTools
for /f "usebackq" %%i in (`vswhere -latest -products %ids% -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%j
)

Starting with version 1.0.40 it gets even more simple.

@echo off

for /f "usebackq" %%I in (`vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%j
)

PowerShell

$ids = 'Community', 'Professional', 'Enterprise', 'BuildTools' | foreach { 'Microsoft.VisualStudio.Product.' + $_ }
$instance = vswhere -latest -products $ids -requires 'Microsoft.Component.MSBuild' -format json `
          | convertfrom-json `
          | select-object -first 1

$path = join-path $instance.installationPath 'MSBuild\15.0\Bin\MSBuild.exe'
if (test-path $path) {
  & $path $args
}

Starting with version 1.0.32, you can simplify this command to return just the property - like installationPath - you require.

$ids = 'Community', 'Professional', 'Enterprise', 'BuildTools' | foreach { 'Microsoft.VisualStudio.Product.' + $_ }
$path = vswhere -latest -products $ids -requires 'Microsoft.Component.MSBuild' -property installationPath

Starting with version 1.0.40 it gets even more simple.

$path = vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath

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