What is Your .NET Framework Version? Use PowerShell to Check

Finding out which .NET Framework Version is currently installed is not something that most people think of every day. However, if you are a  Windows Server Administrator managing web servers that run .NET web applications, then making sure that your servers are running the correct version can be very important!

Using PowerShell to Find the .NET Framework Version Installed on a Windows Computer

So how in the world can you check your server to see if it has the correct .NET Framework version installed? Well, the answer to that is PowerShell! Because the .NET Framework version that is installed on your computer is stored in the Windows Registry, this makes PowerShell the best option to retrieve the information.

But what about regedit?  The great thing about regedit is that it’s readily available, simple to use and presents the registry in a nice application. With that application, you can definitely do the same thing that we’re doing here with PowerShell. Unfortunately for regedit users, it really will take longer to navigate through the registry, find the right key, and then decipher the value to find out what version of .NET Framework is installed.

But with a PowerShell script, you can save it someplace and use it again later.  By doing this, you save yourself the trouble of having to look up the exact location in the registry that you have to find. Also, the registry doesn’t save the .NET Framework version as an easy to read description like “version 4.6.1”.  Instead, it stores the version information as the number of the release. So without the decoder ring, you’re just left trying to figure out what “378389” means.

Finding the .NET Framework Version on a Local System

This part is really simple.  You can copy and paste this code into a PowerShell ISE on the remote system, or save this as a .PS1 file and move it over to the server. Once the script is there, you can run it and it will output the .NET Framework version.

It does this by using a couple of very simple commands. First, it gets the registry key that holds the “release” value.

$NetRegKey = Get-Childitem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'

Next, it gets the value of “Release” from the registry key.

$Release = $NetRegKey.GetValue("Release")

With the vale of the release saved into a variable, it’s easy to take an action based on what the value of that variable is. To do this, we will use a SWITCH statement. The action taken by the SWITCH statement will work like a translator to tell us what .NET Framework version is installed, based on the number in the release value.

Switch ($Release) {
   378389 {$NetFrameworkVersion = "4.5"}
   378675 {$NetFrameworkVersion = "4.5.1"}
   378758 {$NetFrameworkVersion = "4.5.1"}
   379893 {$NetFrameworkVersion = "4.5.2"}
   393295 {$NetFrameworkVersion = "4.6"}
   393297 {$NetFrameworkVersion = "4.6"}
   394254 {$NetFrameworkVersion = "4.6.1"}
   394271 {$NetFrameworkVersion = "4.6.1"}
   394802 {$NetFrameworkVersion = "4.6.2"}
   394806 {$NetFrameworkVersion = "4.6.2"}
   Default {$NetFrameworkVersion = "Net Framework 4.5 or later is not installed."}
}

Finally, we output the results by simply calling the $NetFrameworkVersion variable that was set using the switch.

$NetFrameworkVersion

Easy, right?

The Get-iLPNetFrameworkVersion script as a function

Here is the same commands wrapped up as a function that can be used to find the .NET Framework Version from one or more machines. If it is run without any parameters, then it runs against the local machine and does not use any PowerShell remoting features to operate. However, if you supply the “-Computer” parameter with one or more computers, then a remote PowerShell session is created, and the commands are run on the remote machines. Whether or not the script is run against just the local computer or a bunch of remote systems, the output includes both the computer name and the .NET Framework version.

function Get-iLPNetFrameworkVersion {
[CmdletBinding()]
param(
    [string[]]$Computer = "localhost"
)
$ScriptBlockToRun = {
    $NetRegKey = Get-Childitem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
    $Release = $NetRegKey.GetValue("Release")
    Switch ($Release) {
        378389 {$NetFrameworkVersion = "4.5"}
        378675 {$NetFrameworkVersion = "4.5.1"}
        378758 {$NetFrameworkVersion = "4.5.1"}
        379893 {$NetFrameworkVersion = "4.5.2"}
        393295 {$NetFrameworkVersion = "4.6"}
        393297 {$NetFrameworkVersion = "4.6"}
        394254 {$NetFrameworkVersion = "4.6.1"}
        394271 {$NetFrameworkVersion = "4.6.1"}
        394802 {$NetFrameworkVersion = "4.6.2"}
        394806 {$NetFrameworkVersion = "4.6.2"}
        Default {$NetFrameworkVersion = "Net Framework 4.5 or later is not installed."}
    }
    $Object = [PSCustomObject]@{
        Computername = $env:COMPUTERNAME
        NETFrameworkVersion = $NetFrameworkVersion
    }
    $Object
}
if ($Computer = "localhost") { 
         . $ScriptBlockToRun
} else {
        $Session = New-PSSession $Computer
        Invoke-Command -Session $RemoteSession -ScriptBlock $ScriptBlockToRun
}
}