Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Last active March 2, 2019 22:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpluimers/9b9953050aab8e3e511f to your computer and use it in GitHub Desktop.
Save jpluimers/9b9953050aab8e3e511f to your computer and use it in GitHub Desktop.
Get the path to the most recent msbuild.exe from the registry.
@echo off
:: http://stackoverflow.com/questions/328017/path-to-msbuild
:: http://www.csharp411.com/where-to-find-msbuild-exe/
:: http://timrayburn.net/blog/visual-studio-2013-and-msbuild/
:: http://blogs.msdn.com/b/visualstudio/archive/2013/07/24/msbuild-is-now-part-of-visual-studio.aspx
setlocal
:vswhereModernTry
:: https://github.com/Microsoft/vswhere/wiki/Find-MSBuild
:: Normal output example of `vswhere -legacy -latest -property installationPath` has no trailing back-slash:
:: `C:\Program Files (x86)\Microsoft Visual Studio 14.0\`
for /f "usebackq tokens=*" %%i in (`vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
set InstallDir=%%i
)
:: without ENABLEEXTENSIONS, %InstallDir% is only available outside the above loop.
for %%v in (15.0, 14.0) do (
if exist "%InstallDir%\MSBuild\%%v\Bin\MSBuild.exe" (
set msBuildExe="%InstallDir%\MSBuild\%%v\Bin\MSBuild.exe"
goto :finish
)
)
:manualTry
:: order of the versions is important: get the most recent one
for %%v in (14.0, 12.0, 4.0, 3.5, 2.0) do (
for /f "usebackq tokens=2* delims= " %%c in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\%%v" /v MSBuildToolsPath`) do (
set msBuildExe="%%dMSBuild.exe"
goto :finish
)
)
:vswhereLegacyTry
:: -legacy is not compatible with -products or -requires
:: note there is no Visual Studio 13.0 (just like there is no Office 13.0) likely because USA superstition.
:: https://en.wikipedia.org/wiki/Microsoft_Visual_Studio#History
:: msbuild was introduced in Visual Studio 8.0: https://en.wikipedia.org/wiki/MSBuild#History
:: Legacy output example of `vswhere -legacy -latest -property installationPath` has trailing back-slash:
:: `C:\Program Files (x86)\Microsoft Visual Studio 14.0\`
for /f "usebackq tokens=*" %%i in (`vswhere -legacy -latest -property installationPath`) do (
set InstallDir=%%i
)
:: without ENABLEEXTENSIONS, %InstallDir% is only available outside the above loop.
for %%v in (14.0, 12.0, 11.0, 10.0, 9.0, 8.0) do (
if exist "%InstallDir%MSBuild\%%v\Bin\MSBuild.exe" (
set msBuildExe="%InstallDir%MSBuild\%%v\Bin\MSBuild.exe"
goto :finish
)
)
:: nothing found
:finish
endlocal & if not [%msBuildExe%]==[] if exist %msBuildExe% ( echo %msBuildExe% )
for /f "usebackq tokens=*" %%c in (`"%~dp0get-msbuildExe-path.bat"`) do (
call %%c %*
)
@lextm
Copy link

lextm commented Jan 30, 2016

Time to add 14.0 (part of VS 2015).

@lextm
Copy link

lextm commented Mar 9, 2017

Note that 15.0 (in VS2017) no longer registers itself at this registry key location, so this trick won't simply work. vswhere is now recommended to locate MSBuild 15,

https://github.com/Microsoft/vswhere

@n9
Copy link

n9 commented May 17, 2017

Be sure to call vswhere -products * to get standalone installation of BuildTools. (See microsoft/vswhere#61.)

@jpluimers
Copy link
Author

@lextm, @n9:

Thanks! I have updated the script. I tried to have it find versions prior to 15.0 via vswhere as well, but only as a last resort, as it looks like those put msbuild not inside the Visual Studio directory.

Since -products is incompatible with -legacy, you cannot have it hunt for msbuild in older versions, so the only you can do is look at Visual Studio using vswhere -legacy -latest -property installationPath.

Another uncool thing is that installationPath for -legacy ends with a back-slash, but for non-legacy stuff does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment