Thursday, July 2, 2020

PowerShell: Determining the versions of .NET Installed on Windows

I encountered an issue where I had to 
  1. rollback the .NET Framework to version 4.5.x
  2. install some software 
  3. restore the just removed versions of .NET. 
In order to accomplish this the following PowerShell script was developed based on How to: Determine which .NET Framework versions are installed. The script file was named Get-DotNetVersion.ps1 and is implemented as follows:
<# 
https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed 
To rollback from .NET Framework 4.7.2 (a.k.a. version 461808) on Windows Server 2012 R2 uninstalle the update:  
KB4054566  Microsoft .NET Framework 4.7.2 for Windows 8.1, Windows RT 8.1, and Windows Server 2012 R2 
#>
[string] $dotNetPath = 'HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
[int] $dotNetVersion = (Get-ItemProperty $dotNetPath).Release
if ($dotNetVersion -ge 528040) {
    Write-Host '4.8 or higher'
}

elseif ($dotNetVersion -ge 461808) { Write-Host '4.7.2' } elseif ($dotNetVersion -ge 461308) { Write-Host '4.7.1' } elseif ($dotNetVersion -ge 460798) { Write-Host '4.7' } elseif ($dotNetVersion -ge 394802) { Write-Host '4.6.2' } elseif ($dotNetVersion -ge 394254) { Write-Host '4.6.1' } elseif ($dotNetVersion -ge 393295) { Write-Host '4.6' } elseif ($dotNetVersion -ge 379893) { Write-Host '4.5.2' } elseif ($dotNetVersion -ge 378675) { Write-Host '4.5.1' } elseif ($dotNetVersion -ge 378389) { Write-Host '4.5' }

No comments :

Post a Comment