Get the path to the most recent msbuild.exe from the registry:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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% ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for /f "usebackq tokens=*" %%c in (`"%~dp0get-msbuildExe-path.bat"`) do ( | |
| call %%c %* | |
| ) |
With help from:
- .net – Path to MSBuild – Stack Overflow.
- Where to Find MSBuild.exe : C# 411.
- TimRayburn.net – Visual Studio 2013 and MSBuild.
- MSBuild is now part of Visual Studio! – The Visual Studio Blog – Site Home – MSDN Blogs.
Note
This needs adoption for Visual Studio 2017 (15.0) and up; see the comments at the above gist:
–jeroen






lextm commented on Mar 9, 2017 •
vswhereis now recommended to locate MSBuild 15,https://github.com/Microsoft/vswhere
n9 commented on May 17, 2017
vswhere -products *to get standalone installation of BuildTools. (See Microsoft/vswhere#61.)