Last active
March 2, 2019 22:26
-
-
Save jpluimers/9b9953050aab8e3e511f to your computer and use it in GitHub Desktop.
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 %* | |
) |
Be sure to call vswhere -products *
to get standalone installation of BuildTools. (See microsoft/vswhere#61.)
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
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