Skip to content

Instantly share code, notes, and snippets.

@deviousasti
Created March 11, 2020 20:00
Show Gist options
  • Save deviousasti/a96051f7520898aa5eb99910935a0e21 to your computer and use it in GitHub Desktop.
Save deviousasti/a96051f7520898aa5eb99910935a0e21 to your computer and use it in GitHub Desktop.
Cleanup DotNetSdk
# Discussion at https://github.com/dotnet/sdk/issues/2295
function Uninstall-DotNetSdk {
param (
[parameter(Mandatory = $False)] [switch] $All,
[parameter(Mandatory = $False)] [switch] $WhatIf
)
pushd $env:SystemRoot\System32
Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Where-Object DisplayName -match "^Microsoft .NET Core SDK" |
Where-Object BundleVersion -match "^\d+\.\d+\.\d+\d\d\.\d+$" |
Group-Object { ([regex] "^(\d+\.\d+\.\d+)\d\d\.\d+$").Match($_.BundleVersion).Groups[1].Value } |
Sort-Object { [regex]::Replace($_.Name, '\d+', { param($match) $match[0].Value.PadLeft(10,'0') }) } |
ForEach-Object {
$_.Group |
Sort-Object {
$versionMatch = ([regex] "^\d+\.\d+\.\d+(\d\d)\.(\d+)$").Match($_.BundleVersion)
$v1 = $versionMatch.Groups[1].Value
$v2 = $(if ($versionMatch.Groups[2].Value -eq "0") { "9".PadLeft(10,'9') } else { $versionMatch.Groups[2].Value.PadLeft(10,'0') })
"$v1.$v2"
} |
Select-Object -SkipLast $(if ($All) { 0 } else { 1 }) |
ForEach-Object {
if ($_.QuietUninstallString -imatch '^"?([^"]+)"? +(/uninstall.*)$') {
$file = $Matches[1]
$arglist = $Matches[2]
"Uninstall command: `"$file`" $arglist"
if (-not $WhatIf) { Start-Process -FilePath $file -ArgumentList $arglist -Wait }
}
}
}
popd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment