3

I have a powershell script that looks at a list of VS installations, and determines the highest version installed. It then uses the InstallDir for that version, and uses it to access various commands.

It still uses the lower versions, however.

As of VS2017, it appears that the Registry keys are no longer saved in the same way. I need to update the script to be able to figure out the 2017 settings.

#Add New Versions to this list when new versions of VS are released
$VsVersionsToDisable = "10.0", "11.0", "12.0", "14.0"

[System.Collections.ArrayList]$VsVersions = $VsVersionsToDisable

#Find the Highest installed VS Version, and use it for the TFS.exe Command.
foreach ($version in $VsVersions | Sort-Object -Descending)
{
    $keyPath = "HKCU:\Software\Microsoft\VisualStudio\$version`_Config"
    If (Test-Path $keyPath)
    {
        $aliasPath = Get-ItemProperty -Path $keyPath | Select-Object `
                            -ExpandProperty InstallDir
        $proxyPath = Join-Path $aliasPath "tf.exe"
        set-alias proxyTF $proxyPath
    }
}

To avoid an XY question: We use this script to configure the TFS Proxy settings for a user. It determines the highest installed version, uses it to find the proxy, then iterates through the lower versions configuring their proxy settings with the same value.


What is the best way to determine the installation directory (and also the tf.exe location) for VS2017?

Obsidian Phoenix
  • 4,083
  • 1
  • 22
  • 60

3 Answers3

8

From what I can see, use the SxS\VS7 option:

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7

It should give you the root paths to Visual Studio:

enter image description here

That should get you going.

The tf.exe location is then stored using a symlink under:

.\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Worth pointing out that VS2017 allows multiple installs of the same version (for example, you can have Community, Pro, and Enterprise all installed to different paths), and I'm not sure which one would get the registry value in this case. – Jimmy Feb 24 '17 at 20:22
  • 1
    This approach is perfectly right, this registry entry only list entry corresponding to first type of installation of VS2017. Suppose you first install community edition and then installed enterprise. Registry entry will keep on pointing community edition path as you installed it first. Now if you even uninstall community edition still registry entry will should community edition. – Ajay Yadav Jul 25 '17 at 10:07
  • See this [answer](https://stackoverflow.com/a/43912590/285795) on how to extract the `15.0` value in powershell. – ΩmegaMan Aug 01 '17 at 17:15
3

Since you're using PowerShell, check out https://github.com/microsoft/vssetup.powershell, which is a PS module for detecting installations of VS2017+.

Otherwise, you could need to rely on the Nuget package which is the supported means of detecting VS.

See also this answer on a related question, which predates the PS module I listed above but contains some unsupported methods for finding VS.

Community
  • 1
  • 1
Jimmy
  • 27,142
  • 5
  • 87
  • 100
1

I did use this as a reference and came to a solution in another way. I'm not sure how resilient it is with regards to other versions, but it did the trick for me. It get's the directory of devenv and then I add the extra on the end for TFS. Obviously if the structure is different, then we are screwed. Hope it helps.

    $regKey = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\devenv.exe"
    $visualStudioDir = Get-ItemPropertyValue -Path $regKey -Name "(Default)"
    $visualStudioDir = ($visualStudioDir.Replace("devenv.exe","")).replace("`"","")
    $tfsPath = 'CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe'
    Set-Alias tf $visualStudioDir$tfsPath
    tf workspaces
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
David
  • 21
  • 2