Function Get-DotnetExtraRuntimes { <# .SYNOPSIS Gets extra runtimes for dotnet. Ensure that we only keep the latest around. This assumes that minors are as distinct as majors. Supply the flag RemoveNonMajorDupes to get non-latest minors per major. .PARAMETER RemoveNonMajorDupes Only consider majors as "unique" .PARAMETER ReturnExpiredRuntimes Returns the out of date runtimes .EXAMPLE Get-DotnetExtraRuntimes #> [CmdletBinding()] [OutputType([System.Array])] param( [switch]$RemoveNonMajorDupes, [switch]$ReturnExpiredRuntimes ) $logLead = Get-LogLeadName $deleteRuntimes = @() $allRuntimes = @{} $runtimes = dotnet --list-runtimes Write-Host "$logLead : Dotnet Runtimes installed on local machine:" foreach ($runtime in $runtimes) { Write-Host "$logLead : $runtime" $runtimeParentPath = (($runtime -split '\[')[1] -split '\]')[0] $runtimeApp = ($runtime -split ' ')[0] $runtimeVersion = [System.Version]($runtime -split ' ')[1] $runtimePath = (Join-Path $runtimeParentPath $runtimeVersion) if ($allRuntimes.Keys -notcontains $runtimeApp) { Write-Verbose "$logLead : adding version $runtimePath" $allRuntimes[$runtimeApp] = @{ Version = [System.Version]$runtimeVersion; Path = $runtimePath; }; } if ($allRuntimes[$runtimeApp].Version.Major -lt $runtimeVersion.Major) { } elseif ($allRuntimes[$runtimeApp].Version.Minor -lt $runtimeVersion.Minor -and $RemoveNonMajorDupes) { Write-Verbose "$logLead : adding delete runtime $($allRuntimes[$runtimeApp].Path)" $deleteRuntimes += $allRuntimes[$runtimeApp].Path; } elseif ($allRuntimes[$runtimeApp].Version.Build -lt $runtimeVersion.Build) { Write-Verbose "$logLead : adding delete runtime $($allRuntimes[$runtimeApp].Path)" $deleteRuntimes += $allRuntimes[$runtimeApp].Path; } $allRuntimes[$runtimeApp] = @{ Version = $runtimeVersion; Path = $runtimePath; }; } Write-Host "$logLead : Dotnet Runtimes that should be deleted/out of date:" foreach ($deleteRuntime in $deleteRuntimes) { Write-Host "$logLead : $deleteRuntime" } if ($ReturnExpiredRuntimes) { Return $deleteRuntimes } }