PowerShell script to return versions of .NET Framework on a machine?
What would a PowerShell script be to return versions of the .NET Framework on a machine?
My first guess is something involving WMI. Is there something better?
It should be a one-liner to return only the latest version for each installation of .NET [on each line].
If you're going to use the registry you have to recurse in order to get the full version for the 4.x Framework. The earlier answers both return the root number on my system for .NET 3.0 (where the WCF and WPF numbers, which are nested under 3.0, are higher -- I can't explain that), and fail to return anything for 4.0 ...
EDIT: For .Net 4.5 and up, this changed slightly again, so there's now a nice MSDN article here explaining how to convert the Release value to a .Net version number, it's a total train wreck :-(
This looks right to me (note that it outputs separate version numbers for WCF & WPF on 3.0. I don't know what that's about). It also outputs both Client and Full on 4.0 (if you have them both installed):
Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)p{L}'} |
Select PSChildName, Version, Release
Based on the MSDN article, you could build a lookup table and return the marketing product version number for releases after 4.5:
$Lookup = ConvertFrom-Csv 'Version|Release
4.5|378389
4.5.1|378675
4.5.1|378758
4.5.2|379893
4.6|393295
4.6|393297
4.6.1|394254
4.6.1|394271
4.6.2|394802
4.6.2|394806
4.7|460798
4.7|460805
4.7.1|461308
4.7.1|461310
' -Delimiter "|"
Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)p{L}'} |
Select PSChildName, Version, Release, @{
name = "Product"
expression = {
$Lookup | ? Release -eq $_.Release | % Version
}
}
In fact, since I keep having to update this answer, here's a script to generate the script above from the markdown source for that web page. This will probably break at some point, so I'm keeping the current copy above.
# Get the text from github
$url = "https://raw.githubusercontent.com/dotnet/docs/master/docs/framework/migration-guide/how-to-determine-which-versions-are-installed.md"
$md = Invoke-WebRequest $url -UseBasicParsing
# Replace the weird text in the tables, and the padding
# Then trim the | off the front and end of lines
$map = $md -split "`n" -replace " installed [^|]+" -replace "s+|" -replace "|$" |
# Then select all the lines that start with ".NET Framework"
# and make sure we don't have duplicates
Select-String "^.NET" | Select -Unique
# Then remove the .NET Framework
$map = $map -replace ".NET Framework " -join "`n"
# And just output the script
@"
`$Lookup = ConvertFrom-Csv 'Version|Release
$map
' -Delimiter "|"
Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -Recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { `$_.PSChildName -match '^(?!S)p{L}'} |
Select PSChildName, Version, Release, @{
name="Product"
expression={
`$Lookup | ? Release -eq `$_.Release | % Version
}
}
"@
gci 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' |
sort pschildname -des |
select -fi 1 -exp pschildname
This answer doesn't return 4.5 if that is installed. The answer below from @Jaykul and using recurse does.
[environment]::Version
为您提供PSH正在使用的CLR Version
实例(如此处所述)。