Skip to content

Instantly share code, notes, and snippets.

@Nevember
Created December 26, 2021 03:56
Show Gist options
  • Save Nevember/9d41541acb83bf34e047cecac7131560 to your computer and use it in GitHub Desktop.
Save Nevember/9d41541acb83bf34e047cecac7131560 to your computer and use it in GitHub Desktop.
Function to retrieve .NET version(s) (.NET, Core, and Framework) via Windows Registry
Function Get-dotNetVersion {
<#
.Synopsis
Retrieve .NET version(s) (.NET, Core, and Framework) via Windows Registry
.Description
Retrieve .NET version(s) (.NET, Core, and Framework) via Windows Registry
.Example
Get-dotNetVersion
Version Key
------- ---
6.0.0.21355 HKLM:\Software\Microsoft\ASP.NET Core\Shared Framework\v6.0\6.0.0-preview.6.21355.2
6.0.0.21355 HKLM:\Software\Microsoft\ASP.NET Core\Targeting Pack\v6.0\6.0.0
4.8.9014 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client
4.8.9014 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full
4.0.0.0 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0\Client
3.5.30729.4926 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5
3.0.30729.4926 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0
3.0.30729.4926 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup
3.0.6920.4902 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation
3.0.4506.4926 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Communication Foundation
2.0.50727.4927 HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727
Returns keys for all discovered .NET versions
.Example
(Get-dotNetVersion | Where-Object Version -lt 6.0.0 | Sort-Object Version -Unique -Descending | Select-Object -First 1).Version
Major Minor Build Revision
----- ----- ----- --------
4 8 9014 -1
The wrapped pipeline sends cmdlet results into Where-Object to get versions less than 6
Then, Sort-Object narrows down to unique results, sorted from highest to lowest
Finally Select-Object is used to return only the first result
Outside the pipeline wrapping, the Version property is shown via the member access operator, "."
.Inputs
None.
.Outputs
System.Object[]
.Notes
ABOUT
Name: Get-dotNetVersion
Author: SpiralChaotic
Version: 1.0.0
DateCreated: 2021-December-23
PLATFORMS
PowerShell Core/7/5.1 (Win32NT)
ACKNOWLEDGMENTS
Adapted from the concept and code shared at Mos Tech Tips (see RELATED LINKS).
.Link
https://mostechtips.com/how-to-use-powershell-to-check-net-framework-version
#>
[CmdletBinding()] Param()
$Core = Get-ChildItem 'HKLM:\Software\Microsoft\ASP.NET Core' -Recurse |
Get-ItemProperty -Name Version -ErrorAction 0
$Legacy = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
Get-ItemProperty -Name Version -ErrorAction 0 |
Where-Object {$_.PSChildName -notmatch (Get-Culture).LCID}
$Core + $Legacy |
Select-Object $(
@{
Name = 'Version'
Expression = {
[version] $_.Version
}
}
@{
Name = 'Key'
Expression = {
$_.PSPath -replace "^Microsoft\.PowerShell\.Core\\Registry\:\:HKEY_LOCAL_MACHINE",'HKLM:'
}
}
) |
Sort-Object Version -Descending
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment