-
Notifications
You must be signed in to change notification settings - Fork 99
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.
Note below that the samples 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
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 tokens=*" %%i in (`vswhere -latest -products %ids% -requires Microsoft.Component.MSBuild -property installationPath`) do (
set InstallDir=%%i
)
if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
"%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)
Starting with version 1.0.40 it gets even more simple.
@echo off
for /f "usebackq tokens=*" %%i in (`vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
set InstallDir=%%i
)
if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
"%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)
$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
if (test-path $path) {
& $path $args
}
Starting with version 1.0.40 it gets even more simple.
$path = vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
if (test-path $path) {
& $path $args
}
The asterisk indicates to search all products. It is not, however, a wildcard for pattern matching.
Copyright (C) Microsoft Corporation. All rights reserved. Licensed under the MIT license. See LICENSE.txt in the project root for license information.