PowerShell脚本在计算机上返回.NET Framework版本?

Mat*_*bel 169 .net powershell version

PowerShell脚本在计算机上返回.NET Framework版本会是什么?

我的第一个猜测涉及WMI.还有更好的东西吗?

它应该是一个单行程序,只返回每个.NET [每行]安装的最新版本.

Jay*_*kul 333

如果您要使用注册表,则必须递归才能获得4.x Framework的完整版本.之前的答案都返回我的.NET 3.0系统上的根号(其中WCF和WPF编号嵌套在3.0之下,更高 - 我无法解释),并且无法返回4.0的任何内容. .

编辑:对于.Net 4.5及以上版本,这再次略有改变,所以现在有一篇很好的MSDN文章解释如何将Release值转换为.Net版本号,这是一个完整的火车残骸:-(

这看起来对我来说(注意它在3.0上为WCF和WPF输出单独的版本号.我不知道那是什么).它还在4.0上输出ClientFull(如果你已经安装了它们):

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)\p{L}'} |
Select PSChildName, Version, Release
Run Code Online (Sandbox Code Playgroud)

根据MSDN文章,您可以构建一个查找表并返回4.5之后的版本的营销产品版本号:

$Lookup = @{
    378389 = [version]'4.5'
    378675 = [version]'4.5.1'
    378758 = [version]'4.5.1'
    379893 = [version]'4.5.2'
    393295 = [version]'4.6'
    393297 = [version]'4.6'
    394254 = [version]'4.6.1'
    394271 = [version]'4.6.1'
    394802 = [version]'4.6.2'
    394806 = [version]'4.6.2'
    460798 = [version]'4.7'
    460805 = [version]'4.7'
    461308 = [version]'4.7.1'
    461310 = [version]'4.7.1'
    461808 = [version]'4.7.2'
    461814 = [version]'4.7.2'
    528040 = [version]'4.8'
    528049 = [version]'4.8'
}

# For One True framework (latest .NET 4x), change the Where-Oject match 
# to PSChildName -eq "Full":
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
  Get-ItemProperty -name Version, Release -EA 0 |
  Where-Object { $_.PSChildName -match '^(?!S)\p{L}'} |
  Select-Object @{name = ".NET Framework"; expression = {$_.PSChildName}}, 
@{name = "Product"; expression = {$Lookup[$_.Release]}}, 
Version, Release
Run Code Online (Sandbox Code Playgroud)

事实上,由于我不得不更新这个答案,这里是一个脚本,用于从该网页的降价源生成上面的脚本(有一点额外的).这可能会在某些时候中断,所以我将当前的副本保留在上面.

# 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
$OFS = "`n"
# 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 we can build the table by looking for unique lines that start with ".NET Framework"
    Select-String "^.NET" | Select-Object -Unique |
    # And flip it so it's key = value
    # And convert ".NET FRAMEWORK 4.5.2" to  [version]4.5.2
    ForEach-Object { 
        [version]$v, [int]$k = $_ -replace "\.NET Framework " -split "\|"
        "    $k = [version]'$v'"
    }

# And output the whole script
@"
`$Lookup = @{
$map
}

# For extra effect we could get the Windows 10 OS version and build release id:
try {
    `$WinRelease, `$WinVer = Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ReleaseId, CurrentMajorVersionNumber, CurrentMinorVersionNumber, CurrentBuildNumber, UBR
    `$WindowsVersion = "`$(`$WinVer -join '.') (`$WinRelease)"
} catch {
    `$WindowsVersion = [System.Environment]::OSVersion.Version
}

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
    Get-ItemProperty -name Version, Release -EA 0 |
    # For The One True framework (latest .NET 4x), change match to PSChildName -eq "Full":
    Where-Object { `$_.PSChildName -match '^(?!S)\p{L}'} |
    Select-Object @{name = ".NET Framework"; expression = {`$_.PSChildName}}, 
                @{name = "Product"; expression = {`$Lookup[`$_.Release]}}, 
                Version, Release,
    # Some OPTIONAL extra output: PSComputerName and WindowsVersion
    # The Computer name, so output from local machines will match remote machines:
    @{ name = "PSComputerName"; expression = {`$Env:Computername}},
    # The Windows Version (works on Windows 10, at least):
    @{ name = "WindowsVersion"; expression = { `$WindowsVersion }}
"@
Run Code Online (Sandbox Code Playgroud)

  • Get-ChildItem返回所有注册表子项(子文件夹,如果您愿意).Get-ItemProperty返回值(特别是:"Version"和"Release") - 我们忽略错误,因为我们不关心那些没有这些值的文件夹.所以是的,基本上我们找到每个子文件夹,然后查找版本或版本(任何没有一个或两个文件夹的文件夹都会被忽略). (3认同)
  • 真棒!我只将`(?!S)`子句修改为`(?![SW])`以进一步排除"Windows*"条目.这也可以用`(?= [vCF])`来完成,因为我们真正关心的唯一键是.NET 4.0+的版本根和"完整"和"客户端"键.;) (3认同)
  • @Johnrad`PSChildName`是注册表项的叶名称。“ \ p {L}”是Unicode类别“字母”中的任何字符。`(?!S)`是一个否定的外观,而`^`是字符串的开始。因此,必须以“ S”以外的字母开头。因此,如果只考虑ASCII,则与$ _。PSChildName -cmatch'^ [A-RT-Za-z]'`相同(请注意-cmatch)。因此,它会找到名称以“ S”以外的字母开头的键。我不知道如果您要过滤掉以`S`开头的名称,为什么还要关心非ASCII ...绝对让您感到困惑。 (2认同)

Jas*_*ome 27

gci 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' |
sort pschildname -des                                  |
select -fi 1 -exp pschildname
Run Code Online (Sandbox Code Playgroud)

This answer doesn't return 4.5 if that is installed. The answer below from @Jaykul and using recurse does.

  • gci'HKLM:\ SOFTWARE\Microsoft\NET Framework Setup\NDP'| 排序pschildname -des | foreach-object {$ _.name; .$ _的GetValue( "版本");} (5认同)
  • 在 Windows 10 上安装了 .NET 4.7.1,这仍然返回 v4.0。 (2认同)

Ric*_*ard 22

[environment]::Version
Run Code Online (Sandbox Code Playgroud)

为您提供VersionCLR的当前副本正在使用的实例(如此处所述).

  • 对于Powershell 2.0,您还可以使用`$ PSVersionTable`来查找正在运行的CLR PowerShell的版本. (9认同)
  • 更高版本怎么样?我现在有**.NET 4.7.1**,脚本**总是返回4.0.30319 Rev. 42000.** (6认同)
  • 我安装了.NET 4但PowerShell只使用2.0运行时.所以这不是真正的帮助. (3认同)

小智 22

添加了对脚本的v4.7.2支持:

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?![SW])\p{L}'} |
Select PSChildName, Version, Release, @{
  name="Product"
  expression={
      switch -regex ($_.Release) {
        "378389" { [Version]"4.5" }
        "378675|378758" { [Version]"4.5.1" }
        "379893" { [Version]"4.5.2" }
        "393295|393297" { [Version]"4.6" }
        "394254|394271" { [Version]"4.6.1" }
        "394802|394806" { [Version]"4.6.2" }
        "460798|460805" { [Version]"4.7" }
        "461308|461310" { [Version]"4.7.1" }
        "461808|461814" { [Version]"4.7.2" }
        "528040|528049" { [Version]"4.8" }
        {$_ -gt 528049} { [Version]"Undocumented version (> 4.8), please update script" }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 14

正确的语法:

[System.Runtime.InteropServices.RuntimeEnvironment]::GetSystemVersion()
#or
$PSVersionTable.CLRVersion
Run Code Online (Sandbox Code Playgroud)

GetSystemVersion函数返回如下字符串:

v2.0.50727        #PowerShell v2.0 in Win 7 SP1
Run Code Online (Sandbox Code Playgroud)

或者像这样

v4.0.30319        #PowerShell v3.0 (Windows Management Framework 3.0) in Win 7 SP1
Run Code Online (Sandbox Code Playgroud)

$PSVersionTable是一个只读对象.CLRVersion属性是一个结构化版本号,如下所示:

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      30319  18444   
Run Code Online (Sandbox Code Playgroud)


js2*_*010 12

我通过powershell for osx中的tab完成找到了这个:

[System.Runtime.InteropServices.RuntimeInformation]::get_FrameworkDescription() .NET Core 4.6.25009.03