For that you have to steer vswhere, and seems I’d taken that out of the script because I realized my implementation was a bit too noddy.
# Get the most recent install path (-last 1) from its list of installed locations.
$VisualStudioInstallPath = & $VsWhere -prerelease -latest -property installationPath
A powershell hint: PS stuck with the posix spec for cmd arguments to enable it to do really powerful autocompletion, but that means that arguments are always single - and help is ‘-?’
vswhere -?
Visual Studio Locator version 3.0.3+45247720e1 [query version 3.4.1128.26111]
Copyright (C) Microsoft Corporation. All rights reserved.
Usage: [options]
Options:
...
This is my naive original version of that:
function vsdev () {
Param([string] $Version = "latest")
$VsWhere = Join-Path (Join-Path (Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio') Installer) vswhere.exe
if ( Test-Path $VsWhere ) {
if ($Version -eq "latest") {
$VisualStudioInstallPath = & $VsWhere -prerelease -latest -property installationPath | select -last 1
} else {
$VisualStudioInstallPath = & $VsWhere -prerelease -version "[${Version}.0,$($Version+1).0" -property installationPath | select -last 1
}
Import-Module -ea stop (Join-Path (Join-Path (Join-Path $VisualStudioInstallPath Common7) Tools) Microsoft.VisualStudio.DevShell.dll)
Enter-VsDevShell -VsInstallPath $VisualStudioInstallPath -SkipAutomaticLocation -DevCmdArguments "-arch=amd64 -host_arch=amd64"
} else {
write-error "Could not find ${vswhere}"
}
}
But vswhere supports “-format” options that you could use to do the selection more naturally.
PS> vswhere -format json -prerelease | convertfrom-json
PS> $versions.length
1
PS> $versions[0]
instanceId : d3a837e5
installDate : 11/11/2022 4:40:49 AM
installationName : VisualStudio/17.4.4+33213.308
installationPath : C:\Program Files\Microsoft Visual Studio\2022\Community
installationVersion : 17.4.33213.308
...
PS> $versions[0].installationPath
C:\Program Files\Microsoft Visual Studio\2022\Community
PS> $versions | where installationVersion -eq 17
and then you can use more natural pattern matching:
PS> $want = '17.4.*' ; $versions | where installationVersion -match $want
instanceId : d3a837e5
installDate : 11/11/2022 4:40:49 AM
installationName : VisualStudio/17.4.4+33213.308
installationPath : C:\Program Files\Microsoft Visual Studio\2022\Community
installationVersion : 17.4.33213.308
=== annecdotes ===
It seems a lot of folks who have .net experience are more familiar with working with xml,
PS> [xml]$versions = & $vswhere -prerelease -format xml
# prefix '&' denotes a command line vs an operation on a variable
PS> $versions
xml instances
--- ---------
version="1.0" instances
PS> $versions.instances.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False XmlElement System.Xml.XmlLinkedNode
PS> $versions | get-member
TypeName: System.Xml.XmlDocument
Name MemberType Definition
---- ---------- ----------
ToString CodeMethod static string XmlNode(psobject inst…
AppendChild Method System.Xml.XmlNode AppendChild(Syst…
Clone Method System.Xml.XmlNode Clone(), System.…
CloneNode Method System.Xml.XmlNode CloneNode(bool d…
...