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

26,215

Solution 1

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

Solution 2

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

Solution 3

It looks like VCInstallDir is an environment variable that is independent upon the version of Visual Studio.

echo %VCInstallDir%

That may be used in a batch file.

Share:
26,215
user565739
Author by

user565739

Updated on September 18, 2022

Comments

  • user565739
    user565739 almost 2 years

    Normally, the path is something like C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\.

    From Visual Studio, one can use $(VCInstallDir)$ to get this path.

    Q: But in a batch file, how to get this path?
    

    I know one can use environment variable %VS100COMNTOOLS% in a batch file to get a similar path which is C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\.

    The solution should not dependent on VS's version.

  • Craig Ringer
    Craig Ringer over 10 years
    Pretty sure this will have issues with spaces in install paths. stackoverflow.com/questions/22352793/…
  • Admin
    Admin about 5 years
    Is this variable available after the user has installed MSVC at any time? I am trying to figure out a convenient way to look up an MSVC installation to call it's vcvarsall.bat to get further details and paths for the compilers and linker.
  • Ramhound
    Ramhound almost 5 years
    Why is the link to the non-English version of the documentation?
  • CodeManX
    CodeManX almost 4 years
    This isn't a system wide environment variable, but set by the VS command prompt it seems. In order to access that, you need to find the right command prompt or vcvarsall.bat file, which is basically equivalent to the problem of finding the VS installation path.
  • Ron Burk
    Ron Burk about 3 years
    Note: If you're someone who just installed the Visual Studio "build tools" so you can use the command-line compiler (which you still need to find the path to), vswhere is of no particular use, since it will report nothing is installed.