Programmatically finding the VS2017 installation directory

You can use vswhere tool to get VS2017 location.

Example:

@echo off

rem VS2017U2 contains vswhere.exe
if "%VSWHERE%"=="" set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"

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

You can read more about it here: https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/


Visual Studio 2017 supports no-registry, side-by-side installations of all SKUs (Enterprise, Professional and Community).

MSI installlers can query via APIs described here: https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/

Examples are here:

  • https://code.msdn.microsoft.com/Visual-Studio-Setup-0cedd331
  • https://github.com/microsoft/vs-setup-samples

Well, vswhere.exe doesn't really supply more than the Visual Studio edition installation path. Here's my .profile file Interix snippet from 2008 doing the same with a minor update (shell script):

if [[ -n $PROCESSOR_ARCHITEW6432 || $PROCESSOR_ARCHITECTURE != "x86" ]]; then
  hkeybase='HKLM\SOFTWARE\Wow6432Node\Microsoft\'
else
  hkeybase='HKLM\SOFTWARE\Microsoft\'
fi
for vsver in "15.0" "14.0" "12.0" "11.0" "10.0" "9.0" "8.0"; do
  _vsinstalldir=$(reg.exe query ${hkeybase}'VisualStudio\SxS\VS7' -v $vsver 2>/dev/null \
   | sed -n 's|.*REG_SZ *\([ [:print:]]*\).*|\1|p' | sed 's|\\|/|g')
if [[ -n $_vsinstalldir ]]; then break; fi
done; unset vsver

That's enumerating Visual Studio installations favouring the latest in registry key

HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7

Still working for Visual Studio 2017. Would be easy to translate to cmd syntax. To query the registry is simpler and doesn't require vswhere.exe in your path, thus favourable IMO.

Now finding the current Visual C++ instance and the SDKs is another task entirely. :D

Common output in case you wonder:

C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/

KindDragon's solution didn't quite work for me due to batch's "delayed expansion" "feature". (WAT)

Here is my code, compatible with VS 2017 15.2 (for the vswhere.exe installation)

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat" (
  call "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
) else (
  echo "Could not find !InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
)

Especially note usage of SETLOCAL EnableDelayedExpansion and !InstallDir!