Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use PowerShell to Reset a Visual Studio Experimental Instance
<#
.SYNOPSIS
Resets (or creates with confirmation) the Visual Studio Experimental Instance
.DESCRIPTION
Given the parameters provided, will reset or create (if confirmation is given) the Experimental Instance used for Visual Studio Extensions Development located at the given InstanceName
for the given Version of Visual Studio. If the instance isn't found, you'll be prompted to confirm whether or not you want to create a new instance and the existing instances will
be listed to assist you in case you simply mistyped (or in the case of Visual Studio 2017, discovered that it had a hash in front of it).
.PARAMETER InstanceName
The name of the instance to reset (defined by the RootSuffix parameter, but may vary slightly for Visual Studio 2017). In Visual Studio 2017, this name will sometimes (always?) have a
hash value in front of it
.PARAMETER Version
The "internal version" of Microsoft Visual Studio (15.0 = 2017, 14.0 = 2015, etc) to reset the instance on.
#>
param(
[Parameter(Mandatory=$false, Position=0)][string]$InstanceName,
[Parameter(Mandatory=$false, Position=1)][string]$Version
)
function Get-VisualStudioVersions {
[OutputType([string[]])]
param()
process {
Write-Host "$($retVal.Count)..."
return $retVal.ToArray()
}
}
if ([string]::IsNullOrWhiteSpace($Version)) {
# Attempt to get the version of Visual Studio that's installed, assuming there's only one
$Local:Versions = New-Object System.Collections.Generic.List[string]
foreach($item in (Get-Item 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7' | Select-Object -Property Property).Property) {
$Local:Versions.Add($item)
}
if ($Local:Versions.Count -eq 0) {
Write-Error 'There were no Microsoft Visual Studio versions detected on this machine.'
exit
}
if ($Versions.Count -gt 1) {
Write-Host $Versions.Length
Write-Host "There were $($Versions.Length) versions of Microsoft Visual Studio installed on this machine." -ForegroundColor 'Red'
Write-Host "Which Version would you like to reset for?" -ForegroundColor "White"
$options = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices = New-Object System.Collections.Generic.List[string]
$i=0;
foreach($detectedVersion in $Versions) {
$choices.Add($detectedVersion)
$i += 1
$options.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList "&$i. $detectedVersion"))
Write-Host "$i) $detectedVersion"
}
$options.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList "&C. Cancel"))
$decision = $Host.UI.PromptForChoice("Version Selection", "Select a Version", $options, 1)
if (($decision -gt $choices.Count) -or ($decision -lt 0)) {
Write-Error "Cancelled."
exit
}
$Version = $choices[$decision]
}
else {
Write-Host $Versions
Write-Warning "Auto-selecting version $Local:Versions"
$Version = $Local:Versions
}
}
$InstallPath = "$((Get-ItemProperty 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7' | Select-Object $Version).$Version)"
$CreateExperimentalInstance = [System.IO.Path]::Combine($InstallPath, "VSSDK\VisualStudioIntegration\Tools\Bin\CreateExpInstance.exe")
if (-not (Test-Path $CreateExperimentalInstance -PathType Leaf)) {
Write-Warning "Couldn't find CreateExpInstance.exe at $CreateExperimentalInstance"
Write-Warning "Make sure you have the Visual Studio SDK Installed"
exit
}
# Get the location where Visual Studio Profiles are stored
$VisualStudioInstancePath = [System.IO.Path]::Combine($env:LOCALAPPDATA, "Microsoft\VisualStudio")
Write-Verbose "Looking in instance path: $VisualStudioInstancePath"
if (-not (Test-Path $VisualStudioInstancePath -PathType Container)) {
Write-Error "Couldn't locate the path for the Visual Studio profiles. Expected path was: '$VisualStudioInstancePath'"
exit
}
if ([string]::IsNullOrWhiteSpace($InstanceName)) {
Write-Host "No Experimental Instance was Specified - Select an instance to use and set it as the first parameter of this script"
Get-ChildItem $VisualStudioInstancePath
exit
}
$ExperimentalInstancePath = [System.IO.Path]::Combine($VisualStudioInstancePath, "${Version}$InstanceName")
Write-Verbose "Looking in instance path: $ExperimentalInstancePath"
if (-not (Test-Path $ExperimentalInstancePath -PathType Container)) {
# We'll do one extra check for the "_" version of the path since user input might have been wrong with the way VS2017 handles it
$ExperimentalInstancePath = [System.IO.Path]::Combine($VisualStudioInstancePath, "${Version}_$InstanceName")
$Local:OldInstanceName = $InstanceName
$InstanceName = "_$InstanceName"
Write-Verbose "Also checking instance path: $ExperimentalInstancePath"
if (-not (Test-Path $ExperimentalInstancePath -PathType Container)) {
# Reset it back to the original name the user provided since we couldn't find eiher
$ExperimentalInstancePath = [System.IO.Path]::Combine($VisualStudioInstancePath, "${Version}$InstanceName")
$InstanceName = $Local:OldInstanceName
$Instances = (Get-ChildItem $VisualStudioInstancePath ${Version}*)
if ($Instances.Length -eq 0) {
Write-Error "Couldn't find a Visual Studio profile for Microsoft Visual Studio $Version in '$VisualStudioInstancePath'"
exit
}
Write-Host "The Microsoft Visual Studio $Version experimental instance named '$InstanceName' was not found, however, others were:"
foreach($existingInstance in $Instances) {
Write-Host "$existingInstance"
}
$options = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$options.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$options.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
$decision = $Host.UI.PromptForChoice("Instance Doesn't Exist", "Resetting the instance named '$InstanceName' will create a new experimental instance. Are you sure you want to do that?", $options, 1)
if ($decision -ne 0) {
exit
}
}
}
. $CreateExperimentalInstance /Reset /VSInstance=$Version /RootSuffix=$InstanceName
Write-Host "Experimental Instance for Microsoft Visual Studio $Version named '$InstanceName' was reset."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment