Programmatically finding the VS2017 installation directory

14,377

Solution 1

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/

Solution 2

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:

Solution 3

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!

Solution 4

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/

Solution 5

I had a devil of time trying to modify Srekel's answer to search for only VS2017. Note: If you put the "for" statement below inside an "if" block it will wreck the escape characters and won't work.

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

set vswherestr=^"!ProgramFiles(x86)!\Microsoft Visual Studio\Installer\vswhere.exe^" -version [15.0,16.0^^) -products * -requires Microsoft.Component.MSBuild -property installationPath
for /f "usebackq tokens=*" %%i in (`!vswherestr!`) do (  
  set BUILDVCTOOLS=%%i\Common7\Tools
  echo BUILDVCTOOLS: !BUILDVCTOOLS!
  if not exist !BUILDVCTOOLS!\VsDevCmd.bat (
    echo Error: Cannot find VS2017 Build Tools
    goto :buildfailed
  )
  call "!BUILDVCTOOLS!\VsDevCmd.bat"
)    
Share:
14,377

Related videos on Youtube

Brad
Author by

Brad

Updated on June 04, 2022

Comments

  • Brad
    Brad almost 2 years

    With previous versions of VS you could query the registry to determine the installation directory for VS:

    HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0

    However, this doesn't seem to work with the VS2017 RC. We have scripts that detect the latest installed VS and then do "the right thing", and so far I'm having issues plugging VS2017 into those systems.

    Does anyone know how to programmatically determine the installation location for VS2017?

  • Brad
    Brad over 7 years
    I figured it was some kind of side-by-side thing - thanks for the links!
  • Jason
    Jason about 7 years
    Hey, you tried to sneak the module into my system 😜. Anyway it failed on my system... (-not (Test-Path $vssetup_path -pathType container)) -eq 0 so the module did not install
  • KindDragon
    KindDragon about 7 years
    It's path to VS2015, not 2017
  • DOMZE
    DOMZE about 7 years
    I'm not sure about the new VS2017 version, but possibly change 14.0 to 15.0?
  • KindDragon
    KindDragon about 7 years
    No, VS2017 don't use registry
  • Brad
    Brad almost 7 years
    I'm swapping to this one as the accepted answer as it provides me with an example of how I can script to get the path.
  • Srekel
    Srekel almost 7 years
    This didn't quite work for me - see the changes I needed to do in my answer.
  • Brad
    Brad almost 7 years
    I don't see how delayed expansion factors in to KindDragon's solution - it would only be relevant if the "set" and "get" of the environment variable were nested within a single statement... as it stands his solution works for me
  • Srekel
    Srekel almost 7 years
    I dunno man. I pasted it into my batch file and it didn't work, seemingly because it didn't expand the InstallDir variable at runtime. Then I used EnabledDelayedExpansion and it worked for me. I abhor batch files and wasn't too keen on understanding what went wrong and am happy to just have found a solution :D Posted it in hopes that it might help someone else.
  • N Jones
    N Jones almost 7 years
    This snippet actually works because it doesn't presume vswhere is on the path. For those wondering about the delayed expansion, all it does is enable expansion of variables using the !somevar! syntax within looping and conditional blocks. The currently accepted answer by @KindDragon doesn't use delayed expansion so I presume @Srekel is referring to his own construct not working without it.
  • N Jones
    N Jones almost 7 years
    This works if vswhere is on the path by default which it isn't. See @Srekel's answer below...
  • Srekel
    Srekel almost 7 years
    IIRC, the problem arose when trying to actually use the InstallDir variable. Without delayed expansions, using it with the traditional % lookup doesn't work because then it has the value it has at the beginning of the batch file - i.e. nothing.
  • bytedev
    bytedev over 5 years
    vswhere is a self contained exe so you can always copy it to where ever you want
  • veritas
    veritas over 3 years
    "%VSWHERE%" -latest might not be the best idea if you have both VS2017 and VS2019 installed; you can try "%VSWHERE%" -version [15.0,16.0) though to pinpoint to VS2017