1- # requires -version 5.0
1+ # requires -version 5.0
22# ##############################################################################
33# WintellectPowerShell Module
44# Copyright (c) 2010-2017 - John Robbins/Wintellect
@@ -74,6 +74,10 @@ https://github.com/Wintellect/WintellectPowerShell
7474 [string ]$AdditionalOptions = " "
7575 )
7676
77+ # VS2017+
78+ $vswherePath = " C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
79+
80+ # for VS2017 and older
7781 $versionSearchKey = " HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7"
7882 if ([IntPtr ]::size -ne 8 )
7983 {
@@ -87,20 +91,31 @@ https://github.com/Wintellect/WintellectPowerShell
8791
8892 if ($VSVersion -eq ' Latest' )
8993 {
90- # Find the largest number in the install lookup directory and that will
91- # be the latest version.
92- $biggest = 0.0
93- Get-RegistryKeyPropertiesAndValues $versionSearchKey |
94- ForEach-Object {
95- if ([System.Convert ]::ToDecimal($_.Property , [CultureInfo ]::InvariantCulture) -gt `
96- [System.Convert ]::ToDecimal($biggest , [CultureInfo ]::InvariantCulture))
97- {
98- $biggest = $_.Property
99- $vsDirectory = $_.Value
100- }
101- }
94+ if (Test-Path - PathType Leaf $vswherePath )
95+ {
96+ $latestVSInfo = & $vswherePath - latest - legacy - format json | ConvertFrom-Json
97+ if ($latestVSInfo )
98+ {
99+ $usingVersion = [System.Convert ]::ToDecimal(($latestVSInfo.installationVersion -replace " ^(\d+\.\d+)[^\d].*" , " `$ 1" ), [CultureInfo ]::InvariantCulture)
100+ $vsDirectory = $latestVSInfo.installationPath
101+ }
102+ }
103+ else {
104+ # Find the largest number in the install lookup directory and that will
105+ # be the latest version.
106+ $biggest = 0.0
107+ Get-RegistryKeyPropertiesAndValues $versionSearchKey |
108+ ForEach-Object {
109+ if ([System.Convert ]::ToDecimal($_.Property , [CultureInfo ]::InvariantCulture) -gt `
110+ [System.Convert ]::ToDecimal($biggest , [CultureInfo ]::InvariantCulture))
111+ {
112+ $biggest = $_.Property
113+ $vsDirectory = $_.Value
114+ }
115+ }
102116
103- $usingVersion = $biggest
117+ $usingVersion = $biggest
118+ }
104119 }
105120 else
106121 {
@@ -115,7 +130,16 @@ https://github.com/Wintellect/WintellectPowerShell
115130
116131 $usingVersion = [System.Convert ]::ToDecimal($propVal , [CultureInfo ]::InvariantCulture)
117132
118- if (Test-PathReg - Path $versionSearchKey - Property $propVal )
133+ if (Test-Path - PathType Leaf $vswherePath )
134+ {
135+ $vsInfo = & $vswherePath - version " [${usingVersion} ,$ ( $usingVersion + 1 ) )" - legacy - format json | ConvertFrom-Json
136+ if ($vsInfo )
137+ {
138+ $usingVersion = [System.Convert ]::ToDecimal(($vsInfo.installationVersion -replace " ^(\d+\.\d+)[^\d].*" , " `$ 1" ), [CultureInfo ]::InvariantCulture)
139+ $vsDirectory = $vsInfo.installationPath
140+ }
141+ }
142+ elseif (Test-PathReg - Path $versionSearchKey - Property $propVal )
119143 {
120144 $vsDirectory = (Get-ItemProperty - Path $versionSearchKey - WarningAction SilentlyContinue).$propVal
121145 }
0 commit comments