How To Install and Cleanup Visual Studio 2017 Instances

Visual Studio 2017 allows multiple side by side installations, which makes experimenting with various versions and extensions a breeze:

multiple visual studio in my start menu

To install those multiple IDEs, you simply head over to http://visualstudio.com, grab each of the editions you want to test and just run the installers.

NOTE: you can install one of each VS editions (Community, Professional and Enterprise) as well as one of each VS editions from the Preview “channel” from http://visualstudio.com/preview, for a total of potentially up to 6 instances to play with!

Uninstalling, modifying or even launching the instances is trivial from the Visual Studio Installer UI:

multiple visual studio in my start menu

Sometimes, however, the installer may not be able to modify, repair or even uninstall a given instance. This may happen when adding/removing VSIXes that mess up the so-called catalog that drives the VS installer.

Fortunately, VS provides a tool, called vswhere which can give you comprehensive information about all installed instances.

With it and a bit of powershell, I came up with the following script to allow you to easily cleanup any misbehaving VS instance:

#Requires -RunAsAdministrator
$instances = &"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -format json -prerelease | convertfrom-json
write-host 'Installed instances: '
# Grab longest version to pad properly.
$editionLength = ($instances | select -expand displayName | sort { $_.length } | select -last 1).length
$versionLength = ($instances | select -expand catalog | select -expand productDisplayVersion | sort { $_.length } | select -last 1).length
$instances | % { $i = 0 } { "[$i] {0} - {1} (id {3}) at {2}" -f $_.displayName.padright($editionLength, ' '), $_.catalog.productDisplayVersion.padright($versionLength, ' '), $_.installationPath, $_.instanceId; $i++ }
write-host
$index = read-host 'Enter index of instance to cleanup: '
if ($index -ne '')
{
$instance = $instances | select -skip $index -first 1
if ($instance)
{
if (test-path $instance.installationPath)
{
write-progress -activity "Deleting $instance.installationPath ..."
remove-item -force -recurse $instance.installationPath
}
$instancePath = "C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances\" + $instance.instanceId
if (test-path $instancePath)
{
write-progress -activity "Deleting $instancePath ..."
remove-item -force -recurse $instancePath
}
$instancePath = "C:\PackageCache\_Instances\" + $instance.instanceId
if (test-path $instancePath)
{
write-progress -activity "Deleting $instancePath ..."
remove-item -force -recurse $instancePath
}
$hivePath = $env:LocalAppData + "\Microsoft\VisualStudio\" + $instance.installationVersion.substring(0, 2) + ".0_" + $instance.instanceId
if (test-path $hivePath)
{
write-progress -activity "Deleting $hivePath ..."
remove-item -force -recurse $hivePath
}
$hivePath = $env:LocalAppData + "\Microsoft\VisualStudio\" + $instance.installationVersion.substring(0, 2) + ".0_" + $instance.instanceId + "Exp"
if (test-path $hivePath)
{
write-progress -activity "Deleting $hivePath ..."
remove-item -force -recurse $hivePath
}
write-progress -activity "Done" -completed -status "All done."
}
}
view raw vscleanup.ps1 hosted with ❤ by GitHub

If you want to run it from any Powershell prompt, you can add the following to your powershell profile:

# Add to your profile so you can run `Cleanup-VS` from any PS prompt
Function Cleanup-VS
{
$script = (Split-Path -parent $PSCommandPath) + "\vscleanup.ps1"
&$script
}

Running the Cleanup-VS from a prompt will show you something like the following:

C:\Windows\System32> Cleanup-VS
Installed instances:
[0] Visual Studio Community 2017  - 15.6.1 Preview 1.0                 at C:\Program Files (x86)\Microsoft Visual Studio\External\Pre\Community
[1] Visual Studio Enterprise 2017 - 15.6.3                             at C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise
[2] Visual Studio Enterprise 2017 - 15.7.0 Preview 2.0 [27517.0.d15.7] at C:\Program Files (x86)\Microsoft Visual Studio\External\IntPre

Enter index of instance to cleanup: 

After entering the instance index to clean up, and provided you’re running as an administrator, the entire installation folder and the instance data will be deleted. I have only neeeded this sporadically, but I keep forgetting what to delete from where, so I just scripted it once and for all!

Enjoy :)

Tags: visualstudio

/kzu dev↻d