Getting installed .NET Framework versions is (surprisingly) not a simple task.
On this post, let's try to address that.
When .NET developers have to fix problems on a remote server that requires
knowing which versions of the .NET Framework are installed, they'll be
frustrated. Unfortunately, the
Redmond folks made it
unnecessarily complicated to know the installed versions of the .NET Framework
on a Windows box.
Listing the installed folders
The simplest and fastest solution is obviously listing the contents of the folders .NETFramework folder with:
dir "C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\.NETFramework"
While that's usually sufficient to list what's installed, it does not confirm
IF the frameworks are correctly registered in the system.
To confirm that everything's correctly setup we'll have to use PowerShell as described next.
Listing the frameworks using PowerShell
To list which frameworks are installed directly from the Windows Registry can be found on this PowerShell script:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###################################################################### | |
# list-dotnet-frameworks.ps1 | List installed .NET Framework versions | |
###################################################################### | |
# Source: https://blog.hildenco.com/2020/01/determining-installed-net-framework.html | |
# It may be required to run Powershell with the following command line: | |
# Powershell.exe -executionpolicy remotesigned -File | |
function Get-Installed-Framework-Versions() { | |
$installedFrameworks = @() | |
if (Is-Key-Present "HKLM:\Software\Microsoft\.NETFramework\Policy\v1.0" "3705") { $installedFrameworks += "Installed .Net Framework 1.0" } | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install") { $installedFrameworks += "Installed .Net Framework 1.1" } | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install") { $installedFrameworks += "Installed .Net Framework 2.0" } | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess") { $installedFrameworks += "Installed .Net Framework 3.0" } | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v3.5" "Install") { $installedFrameworks += "Installed .Net Framework 3.5" } | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Client" "Install") { $installedFrameworks += "Installed .Net Framework 4.0c" } | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full" "Install") { $installedFrameworks += "Installed .Net Framework 4.0" } | |
return $installedFrameworks | |
} | |
function Get-Framework-Versions-And-Handle-Operation() { | |
$version = Get-Framework40-Family-Version; | |
if ($version -ge 1) { | |
} | |
else { | |
Download-And-Install-Framework | |
} | |
} | |
function Get-Framework40-Family-Version() { | |
$result = -1 | |
if (Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Client" "Install" -or Is-Key-Present "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full" "Install") { | |
# .net 4.0 is installed | |
$result = 0 | |
$version = Get-Framework-Value "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full" "Release" | |
if ($version -ge 461808 -Or $version -ge 461814) { | |
# .net 4.7.2 | |
Write-Host "Installed .Net Framework 4.7.2" | |
$result = 9 | |
} | |
elseif ($version -ge 461308 -Or $version -ge 461310) { | |
# .net 4.7.1 | |
Write-Host "Installed .Net Framework 4.7.1" | |
$result = 8 | |
} | |
elseif ($version -ge 460798 -Or $version -ge 460805) { | |
# .net 4.7 | |
Write-Host "Installed .Net Framework 4.7" | |
$result = 7 | |
} | |
elseif ($version -ge 394802 -Or $version -ge 394806) { | |
# .net 4.6.2 | |
Write-Host "Installed .Net Framework 4.6.2" | |
$result = 6 | |
} | |
elseif ($version -ge 394254 -Or $version -ge 394271) { | |
# .net 4.6.1 | |
Write-Host "Installed .Net Framework 4.6.1" | |
$result = 5 | |
} | |
elseif ($version -ge 393295 -Or $version -ge 393297) { | |
# .net 4.6 | |
Write-Host "Installed .Net Framework 4.6" | |
$result = 4 | |
} | |
elseif ($version -ge 379893) { | |
# .net 4.5.2 | |
Write-Host "Installed .Net Framework 4.5.2" | |
$result = 3 | |
} | |
elseif ($version -ge 378675) { | |
# .net 4.5.1 | |
Write-Host "Installed .Net Framework 4.5.1" | |
$result = 2 | |
} | |
elseif ($version -ge 378389) { | |
# .net 4.5 | |
Write-Host "Installed .Net Framework 4.5" | |
$result = 1 | |
} | |
} | |
else { | |
# .net framework 4 family isn't installed | |
$result = -1 | |
} | |
return $result | |
} | |
# All .Net Frameworks possible to download here: https://www.microsoft.com/net/download/dotnet-framework-runtime | |
function Download-And-Install-Framework() { | |
# net 4.0 -> http://download.microsoft.com/download/1/B/E/1BE39E79-7E39-46A3-96FF-047F95396215/dotNetFx40_Full_setup.exe | |
# net 4.5 -> http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe | |
# net 4.5.2 -> http://download.microsoft.com/download/E/2/1/E21644B5-2DF2-47C2-91BD-63C560427900/NDP452-KB2901907-x86-x64-AllOS-ENU.exe | |
# net 4.6 -> http://download.microsoft.com/download/1/4/A/14A6C422-0D3C-4811-A31F-5EF91A83C368/NDP46-KB3045560-Web.exe => except win 10 | |
# net 4.7.2 -> https://download.microsoft.com/download/3/3/2/332D9665-37D5-467A-84E1-D07101375B8C/NDP472-KB4054531-Web.exe | |
$url = "https://download.microsoft.com/download/3/3/2/332D9665-37D5-467A-84E1-D07101375B8C/NDP472-KB4054531-Web.exe" | |
$output = "$PSScriptRoot\netFrameworkInstaller.exe" | |
$start_time = Get-Date | |
Invoke-WebRequest -Uri $url -OutFile $output | |
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" | |
Start-Process netFrameworkInstaller.exe | |
# -NoNewWindow -Wait | |
} | |
function Is-Key-Present([string]$path, [string]$key) { | |
if (!(Test-Path $path)) { return $false } | |
if ((Get-ItemProperty $path).$key -eq $null) { return $false } | |
return $true | |
} | |
function Get-Framework-Value([string]$path, [string]$key) { | |
if (!(Test-Path $path)) { return "-1" } | |
return (Get-ItemProperty $path).$key | |
} | |
# print all installed net frameworks | |
Get-Installed-Framework-Versions | |
# in case .net framework family 4 is present in system, script start application otherwise | |
# download and install predefined framework | |
Get-Framework-Versions-And-Handle-Operation |
Simplifying Further
Ok, but let's assume that we're on a remote server. Opening chrome and downloading this script might not be ideal. Could we somehow script this out? Yes, by using PowerShell's Invoke-WebRequest (PowerShell's curl), we could make it a one-liner so that we don't have to even open a browser and download that script from GitHub. Here is it:
Invoke-WebRequest -Uri
https://github.com/hd9/powershell/raw/master/list-dotnet-frameworks.ps1
-OutFile list-dotnet-frameworks.ps1; .\list-dotnet-frameworks.ps1
Hope it helps!See Also
- My journey to 1 million articles read
- PowerShell - The server committed a protocol violation
- Getting last modified software on Windows using PowerShell
- Creating ASP.NET Core websites with Docker
- Send emails from ASP.NET Core websites using SendGrid and Azure
- Configuration in .NET Core console applications
- Countdown to .NET 5.0
- Creating a Ubuntu Desktop instance on Azure
- Package Management in .NET Core
- Building and Running ASP.NET Core apps on Linux
- Why I use Fedora Linux
- Windows Subsystem for Linux, the best way to learn Linux on Windows
- How I fell in love with i3