How to get the Visual Studio installation path in a batch file

Use vswhere utility which is a part of VS bundle (officially), but can also be used apart of MSVC.

> vswhere.exe -latest -property installationPath
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community

Here you may find more details


You could also use the registry to find the path to the Visual Studio install directory. You would have to add extra logic to handle different versions of VS that might be installed e.g 10.0 or 11.0.

@ECHO OFF
setlocal ENABLEEXTENSIONS
; 32-bit system:
set KEY_NAME="SOFTWARE\Microsoft\VisualStudio\9.0\Setup\VS"
; 64-bit system:
; set KEY_NAME="SOFTWARE\WOW6432Node\Microsoft\VisualStudio\9.0\Setup\VS"
set VALUE_NAME=ProductDir

FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set ValueValue=%%C
)

if defined ValueName (
    @echo Registry Value = %ValueValue%
) else (
    @echo %KEY_NAME%\%VALUE_NAME% not found.
)
pause

Way late to this question, but i've found a simpler way to get the MSVC directory. The trick is to use %VS100COMNTOOLS% variable (or the version of your visual studio, here 100 is 10.0), which is guaranteed to exist even without calling the ..\VC\vcvarsall.bat batch file.

%VCInstallDir% variable falls to this as it's empty until vcvarsall.bat is called, but we can't call the file if we don't know the full path.

The %VS100COMNTOOLS% on the other hand exists and returns something like:

c:\Program Files\Microsoft Visual Visual Studio 10.0\Common7\Tools

Then, a simply cut-off of the last characters seems good:

echo "%VS100COMNTOOLS:~0,-14%VC\"