Public/Get-DotNetFrameworkVersion.ps1

Function Get-DotNetFrameworkVersion {
    <#
        .SYNOPSIS
            Get the installed .Net Framework version
 
        .DESCRIPTION
            Get the installed .Net Framework version based on the buildnumber and information provided by Microsoft on
            https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies
 
        .PARAMETER ComputerName
            The computername
 
        .NOTES
            Author : Martin Schvartzman
            Keywords : NETFX, Registry
            Reference : https://msdn.microsoft.com/en-us/library/hh925568
            Updated by : Casper Stekelenburg
 
        .EXAMPLE
            PS C:\> Get-DotNetFramework -ComputerName SERVER01
 
            ComputerName NetFXBuild NetFXVersion
            ------------ ---------- ------------
            SERVER01 50727 .NET Framework 2.0
            SERVER01 30729 .NET Framework 3.0
            SERVER01 30729 .NET Framework 3.5
            SERVER01 461808 .NET Framework 4.7.2 (Windows 10 April 2018 Update and Windows Server, version 1803)
 
        .LINK
            about_functions_advanced
        .LINK
            about_CommonParameters
    #>

    [CmdletBinding()]
    [OutputType('JBOAPPLS.DotNet.Framework.Information')]
    Param(
        [Parameter()]
        [Alias('CN', 'Name', 'Computer')]
        [String[]]$ComputerName = $env:COMPUTERNAME
    )
    Begin {
        If (-not $PSBoundParameters.ContainsKey('Verbose')) {
            $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
        }
        If (-not $PSBoundParameters.ContainsKey('ErrorAction')) {
            $ErrorActionPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ErrorActionPreference')
        }

        $dotNetRegistry = 'SOFTWARE\Microsoft\NET Framework Setup\NDP'
        $dotNet4Registry = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
        $dotNet4Builds = @{
            30319  = '.NET Framework 4.0'
            378389 = '.NET Framework 4.5'
            378675 = '.NET Framework 4.5.1 (8.1/2012R2)'
            378758 = '.NET Framework 4.5.1 (8/7 SP1/Vista SP2)'
            379893 = '.NET Framework 4.5.2'
            380042 = '.NET Framework 4.5 and later with KB3168275 rollup'
            393295 = '.NET Framework 4.6 (Windows 10)'
            393297 = '.NET Framework 4.6 (NON Windows 10)'
            394254 = '.NET Framework 4.6.1 (Windows 10 November Update)'
            394271 = '.NET Framework 4.6.1 (NON Windows 10)'
            394802 = '.NET Framework 4.6.2 (Windows 10 Anniversary Update and Windows Server 2016)'
            394806 = '.NET Framework 4.6.2 (NON Windows 10)'
            460798 = '.NET Framework 4.7 (Windows 10 Creators Update)'
            460805 = '.NET Framework 4.7 (NON Windows 10'
            461308 = '.NET Framework 4.7.1 (Windows 10 Creators Update and Windows Server, version 1709)'
            461310 = '.NET Framework 4.7.1 (NON Windows 10)'
            461808 = '.NET Framework 4.7.2 (Windows 10 April 2018 Update and Windows Server, version 1803)'
            461814 = '.NET Framework 4.7.2 (NON Windows 10)'
            528040 = '.NET Framework 4.8 (Windows 10 May 2019 Update)'
            528049 = '.NET Framework 4.8 (NON Windows 10)'
        }
    }
    Process {
        Foreach ($Computer in $ComputerName) {
            Write-Verbose ('{0}:: Processing {1}' -f $MyInvocation.MyCommand, $Computer)
            Try {
                If ($regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer)) {
                    If ($netRegKey = $regKey.OpenSubKey("$dotNetRegistry")) {
                        ForEach ($versionKeyName in $netRegKey.GetSubKeyNames()) {
                            Write-Verbose ('{0}:: Processing {1} on {2}' -f $MyInvocation.MyCommand, $versionKeyName, $Computer)
                            If ($versionKeyName -match '^v[123]') {
                                $versionKey = $netRegKey.OpenSubKey($versionKeyName)
                                $version = [version]($versionKey.GetValue('Version', ''))
                                New-Object -TypeName PSObject -Property @{
                                    PSTypeName   = 'JBOAPPLS.DotNet.Framework.Information'
                                    ComputerName = $Computer
                                    NetFXBuild   = $version.Build
                                    NetFXVersion = '.NET Framework ' + $version.Major + '.' + $version.Minor
                                }
                            }
                        }
                    }
                    If ($net4RegKey = $regKey.OpenSubKey("$dotNet4Registry")) {
                        If (-not ($net4Release = $net4RegKey.GetValue('Release'))) {
                            $net4Release = 30319
                        }
                        New-Object -TypeName PSObject -Property @{
                            PSTypeName   = 'JBOAPPLS.DotNet.Framework.Information'
                            ComputerName = $Computer
                            NetFXBuild   = $net4Release
                            NetFXVersion = $dotNet4Builds[$net4Release]
                        }
                    }
                }
            } Catch {
                New-Object -TypeName PSObject -Property @{
                    PSTypeName   = 'JBOAPPLS.DotNet.Framework.Information'
                    ComputerName = $Computer
                    NetFXBuild   = $null
                    NetFXVersion = $null
                }
            }
        }
    }
    End {
    }
}