24

I need to run scripts that build a visual studio solutions using devenv.exe (or devenv.com for that matter). For visual studio 2015 there was an environment variable %VS140COMNTOOLS% that I could use to find the install location of devenv. Since there is no %VS150COMNTOOLS% for Visual Studio 2017, what would be a reliable way to find the install location of devenv in a script (bat or powershell).

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
bitbonk
  • 48,890
  • 37
  • 186
  • 278
  • 3
    See: https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/ – jessehouwing Mar 10 '17 at 08:13
  • 1
    Possible duplicate of [Programmatically finding the VS2017 installation directory](http://stackoverflow.com/questions/41106407/programmatically-finding-the-vs2017-installation-directory) – KindDragon Apr 09 '17 at 23:02

7 Answers7

14

One way is to use power shell and vswhere.exe. But I'm bit lazy to install new tools and ...

I was trying to find simpler solution and found it from registry - there exists registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7, which lists all Visual studio installations.

One of limitations mentioned in this link: https://developercommunity.visualstudio.com/content/problem/2813/cant-find-registry-entries-for-visual-studio-2017.html

If there is more than one edition of 2017 installed, then it seems the last one installed will have their path in this key.

But typically you install only one visual studio for build or use purpose.

Also I've coded this sample from 64-bit machine perspective, I think Wow6432Node does not exits in 32-bit machines, but really - how many developers use 32-bit machines nowadays ?

So if you're fine with limitations above, here is a simple batch which can query visual studio installation path:

test.bat :

@echo off
setlocal 
call:vs%1 2>nul
if "%n%" == "" (
    echo Visual studio is not supported.
    exit /b
)
for /f "tokens=1,2*" %%a in ('reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" /v "%n%.0" 2^>nul') do set "VSPATH=%%c"
if "%VSPATH%" == "" (
    echo Visual studio %1 is not installed on this machine
    exit /b
)

echo Visual studio %1 path is "%VSPATH%"
endlocal & exit /b

:vs2017
    set /a "n=%n%+1"
:vs2015
    set /a "n=%n%+2"
:vs2013
    set /a "n=%n%+1"
:vs2012
    set /a "n=%n%+1"
:vs2010
    set /a "n=%n%+10"
    exit /b

Can be executed like this:

>test 2010
Visual studio 2010 path is "C:\Program Files (x86)\Microsoft Visual Studio 10.0\"

>test 2012
Visual studio 2012 path is "C:\Program Files (x86)\Microsoft Visual Studio 11.0\"

>test 2013
Visual studio 2013 path is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\"

>test 2014
Visual studio is not supported.

>test 2015
Visual studio 2015 path is "C:\Program Files (x86)\Microsoft Visual Studio 14.0\"

>test 2017
Visual studio 2017 path is "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\"
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
  • On MSFT Windows 10 x86-64 \`\`reg'' utility has `/reg:32` command-line option (I did not check presence of the switch on more older version of this OS). Therefore, you can add `-reg:32` and remove \`\`WOW6432Node'' from the Registry key specified your script. **But** still I do not know if the `-reg:32` option is available for \`\`reg'' on MSFT Windows 10 x86 :-) – Ruslan Garipov Oct 10 '18 at 13:53
11

You can use vswhere.exe or powershell to find your Visual Studio instances:

for /r "usebackq tokens=1* delims=: " %%i in (`vswhere.exe -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop`) do (
    if /i "%%i"=="installationPath" set dir=%%j
)

and

Install-Module VSSetup -Scope CurrentUser
Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64

The path to specific workloads can be found through this api as well.

https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 12
    "vswhere" cannot be found. Guessing its not in the path, so we have to go to where it is, except that means we know where VS is installed so no point in looking it up with vswhere – StingyJack Jul 01 '17 at 20:06
  • 2
    It looks like the new directory structure for Visual Studio will have a top-level directory `C:\Program Files (x86)\Microsoft Visual Studio` and the various different tools will be installed under that (for example, there's a `2017` directory under there. Also provided is an `Installer` directory, and the `vswhere` tool lives in that. So it's not per-release. Of course that doesn't help if you don't install in the usual place. But it appears (minimal testing) you can copy vswhere anywhere and it still works. It's still strictly worse than before & doesn't work with older VS installs. – MadScientist Jan 20 '18 at 19:53
  • And for completeness here is the full command to get the actual installation path with powershell: `$VSInstallPath = (Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64 | Select-Object -first 1).InstallationPath` – bitbonk Jan 08 '19 at 10:10
4

Here's a PowerShell function that can be used to get the DevEnv.exe path. You can also easily modify it to find other common components, like MsBuild.exe or TF.exe simply by changing the hardcoded DevEnv.exe string used in the -Filter.

function Get-DevEnvExecutableFilePath
{
    [bool] $vsSetupExists = $null -ne (Get-Command Get-VSSetupInstance -ErrorAction SilentlyContinue)
    if (!$vsSetupExists)
    {
        Write-Verbose "Installing the VSSetup module..."
        Install-Module VSSetup -Scope CurrentUser -Force
    }
    [string] $visualStudioInstallationPath = (Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.Component.MSBuild).InstallationPath

    $devEnvExecutableFilePath = (Get-ChildItem $visualStudioInstallationPath -Recurse -Filter "DevEnv.exe" | Select-Object -First 1).FullName
    return $devEnvExecutableFilePath
}

It works by installing the VSSetup module if it's not already present, and then querying that module for the latest Visual Studio installation path. From there, it searches the Visual Studio installation path for the DevEnv.exe file and returns its full path.

deadlydog
  • 22,611
  • 14
  • 112
  • 118
1

My own solution using registry here does not work unfortunately for Visual studio 2019, for it it's possible to query vs2019 installation path from Uninstall registry path, but is starts to be little bit difficult as you might need to walk through sub registry keys.

I have tried also a little bit vswhere.exe, but it's usage is also bit difficult - need to reparse it's output.

Meanwhile I've needed something similar to vswhere, and took a glance look on it's implementation and re-wrote it under my own command line tool, called cppexec.exe.

Usage is like this:

D:\Prototyping\cppscriptcore>cppexec.exe -location
Visual studio 2019:
location: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise

Visual studio 2017:
location: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2017
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2019
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise

To use it, download two files:

https://github.com/tapika/cppscriptcore/blob/master/cppexec.exe https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel.dll

If you're interested in integrating into your own project, take a look on following source codes:

https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.cpp https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.h

I've recoded vswhere into slightly simpler manner.

TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
0

I found a very simple way, just run the Visual Studio Developer Command Prompt, and run this command: where devenv

It will return the full path for devenv.com and devenv.exe file

alaneo
  • 89
  • 4
  • Only relevant window I can find in VS is called "Command Window" in and that says >where devenv Command "where" is not valid. – PandaWood May 10 '21 at 23:58
  • I think that depends on the VS version then, Im using the Enterprise 2019 and its under Tools -> Command Line -> Developer Command Prompt – alaneo May 12 '21 at 12:48
  • Developer command prompt is also available from the Windows start menu since many years. – pvoosten Aug 03 '21 at 11:59
0

Location for Visual Studio 2022 : C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE

Bhushan Jagtap
  • 101
  • 1
  • 6
0

None of the other solutions worked for me. I want to do this with a simple snippet in a bat/cmd file (no powershell), with no external tools, and without depending on the vcvars32 or other bat files having been run already.

The idea of using vswhere has been mentioned by others, but they were not including the path to the tool, and syntax for parsing the output seemed to be off.

The following snippet works for me:

for /f "usebackq tokens=1* delims=: " %%i in (`"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -latest`) do (
  if /i "%%i"=="productPath" set devenv=%%j
)
echo Using Visual Studio: %devenv%
"%devenv%" MySolution.sln &
wiesener
  • 428
  • 4
  • 9