Skip to content

Instantly share code, notes, and snippets.

@heaths
Created May 17, 2019 20:42
Show Gist options
  • Save heaths/aa43288e13b4ad936e30b9fd4ca56efe to your computer and use it in GitHub Desktop.
Save heaths/aa43288e13b4ad936e30b9fd4ca56efe to your computer and use it in GitHub Desktop.
Uninstall superseded .NET Core SDKs that should no longer be needed
#Requires -Version 4.0
#Requires -RunAsAdministrator
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
param (
[Parameter()]
[switch] $Force
)
$yesToAll = $false
$noToAll = $false
Get-ChildItem 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall', 'Registry::HKEY_LOCAL_MACHINE\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' -ErrorAction SilentlyContinue `
| Get-ItemProperty `
| Where-Object { $_.SystemComponent -ne 1 -and $_.DisplayName -like '*.NET Core*' -and $_.DisplayName -notlike '*Shared Framework*' -and [Version]$_.DisplayVersion -ge [Version]'2.0' } `
| Select-Object DisplayName, DisplayVersion, UninstallString, QuietUninstallString, @{l='Version'; e={
$start = $_.DisplayName.IndexOfAny([char]'0'..[char]'9')
$end = $_.DisplayName.IndexOfAny([char[]]([char]' ', [char]'-'), $start)
$version = [Version]$_.DisplayName.Substring($start, $end - $start)
[Version]::new($version.Major, $version.Minor, $version.Build / 100)
}} `
| Group-Object Version `
| Where-Object Count -gt 1 `
| Sort-Object Version `
| ForEach-Object {
$versions = $_.Group | Sort-Object DisplayVersion -Descending
$latest, $superseded = $versions[0], $versions[1..$versions.length]
foreach ($version in $superseded) {
$caption = "Uninstall '{0}'" -f $version.DisplayName
$message = "Uninstall '{0}' superseded by '{1}'" -f $version.DisplayName, $latest.DisplayName
if ($PSCmdlet.ShouldProcess($message, "$message?", $caption)) {
if ($Force -or $PSCmdlet.ShouldContinue("$message?", $caption, [ref] $script:yesToAll, [ref] $script:noToAll)) {
$command = $version.QuietUninstallString
if (!$command) {
$command = $version.UninstallString
}
Start-Process -NoNewWindow -Wait -FilePath 'cmd.exe' -ArgumentList "/c $command"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment