The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,576 other followers

Archive for October 6th, 2015

Get the path to the most recent msbuild.exe from the registry.

Posted by jpluimers on 2015/10/06

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 %*
)

view raw

run-msbuild.bat

hosted with ❤ by GitHub

With help from:

Note

This needs adoption for Visual Studio 2017 (15.0) and up; see the comments at the above gist:

lextm commented on 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 commented on May 17, 2017

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

–jeroen

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C# 6 (Roslyn), Delphi, Delphi 10 Seattle, Delphi 2007, Delphi 2009, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Delphi XE8, Development, Software Development | Leave a Comment »

 
%d bloggers like this: