ChatGPT解决这个技术问题 Extra ChatGPT

如何从 PowerShell 命令行查找 Windows 版本

如何找到我正在使用的 Windows 版本?

我正在使用 PowerShell 2.0 并尝试过:

PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha
t the path is correct and try again.
At line:1 char:4
+ ver <<<< 
    + CategoryInfo          : ObjectNotFound: (ver:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我该怎么做呢?

如果您在 2019 年及以后查看此内容,请忽略标记为正确的答案并直接转到 the one that is correct。别客气。

P
Peter Mortensen

由于您可以访问 .NET 库,因此您可以访问 System.Environment 类的 OSVersion 属性来获取此信息。对于版本号,有 Version 属性。

例如,

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
6      1      7601   65536

可以在 here 中找到 Windows 版本的详细信息。


请注意,[Environment]::OSVersionwindows-10 中有效,OSVersion.Version.Major 返回 10。
当我运行 winver 时,它显示版本 1607。但上面的 powershell 命令没有给出 1607。我在 Powershell 中哪里可以得到这个“1607”数字?
@CMCDragonkai (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
此方法自 Windows 8.1 起已弃用。有关详细信息,请参阅此 link
@SlogmeisterExtraordinaire 命令 [System.Environment]::OSVersion 未被弃用,它在后台使用的方法已被弃用。新的 PS 版本正在改变后端行为:github.com/PowerShell/PowerShell/issues/…
C
Community

要获取 Windows 版本号,正如 Jeff 在他的回答中指出的那样,使用:[Environment]::OSVersion 值得注意的是,结果是 [System.Version] 类型,因此可以检查 Windows 7 /Windows Server 2008 R2 及更高版本带有 [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1) 但是这不会告诉您它是客户端还是服务器 Windows,也不会告诉您版本的名称。使用 WMI 的 Win32_OperatingSystem 类(始终为单个实例),例如:(Get-WmiObject -class Win32_OperatingSystem)。Caption 将返回类似于 Microsoft® Windows Server® 2008 Standard 的内容


Get-WmiObject 的警告:有时需要几秒钟才能返回
[环境]::OSVersion 不可靠devblogs.microsoft.com/scripting/…
A
Anton Krouglov

不幸的是,大多数其他答案都没有提供特定于 Windows 10 的信息。

Windows 10 有自己的 versions1507, 1511, 1607, 1703, etc。这就是 winver 显示的内容。

Powershell:
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId

Command prompt (CMD.EXE):
Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ReleaseId

另见related question on superuser

至于其他 Windows 版本,请使用 systeminfo。 Powershell 包装器:

PS C:\> systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List


OS Name             : Microsoft Windows 7 Enterprise
OS Version          : 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer     : Microsoft Corporation
OS Configuration    : Standalone Workstation
OS Build Type       : Multiprocessor Free
System Type         : x64-based PC
System Locale       : ru;Russian
Hotfix(s)           : 274 Hotfix(s) Installed.,[01]: KB2849697,[02]: KB2849697,[03]:...

同一命令的 Windows 10 输出:

OS Name             : Microsoft Windows 10 Enterprise N 2016 LTSB
OS Version          : 10.0.14393 N/A Build 14393
OS Manufacturer     : Microsoft Corporation
OS Configuration    : Standalone Workstation
OS Build Type       : Multiprocessor Free
System Type         : x64-based PC
System Directory    : C:\Windows\system32
System Locale       : en-us;English (United States)
Hotfix(s)           : N/A

这在桌面上很容易记住 winver,在服务器上很容易记住 systeminfo。多年来,我一直感到困惑的是,没有统一的方式来获取这些信息。
链接到真正有用的 MS 信息。应该注意的是,对于 Win8.1(及以下?),显示的信息是:OS Version : 6.3.9600 N/A Build 9600。因此,在 W81 以下的版本中,查看(总是被忽视的)LTSB 版本可能会提供更多信息。查看来自:(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx 的输出,可能类似于:9600.19179.amd64fre.winblue_ltsb_escrow.181015-1847。我的猜测181015 部分是构建日期,而 1847 是构建或发布版本。您可能还需要将其与 kernel, hal 进行比较。
应该是 2020 年被接受的答案。嘿,SO Team,我们可以改变多年来接受的答案吗?这会是一个 meta 问题吗?
releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在
我在 Windows 11 上,并认为上面注册表中的 BuildLabEx 就是它,但实际上它仍然报告一年前的(RTM?)构建 22000.1 + 实验室字符串,而不是根据 winver 报告我当前的构建 22000.675。 CurrentVersion 键中的任何内容都不会将我当前的内部版本号报告为完整字符串的一部分。只有值 UBR 将报告正确的整数 675,但不是以完整构建字符串的形式。
L
Lars Fosdal
Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

返回

WindowsProductName    WindowsVersion OsHardwareAbstractionLayer
------------------    -------------- --------------------------
Windows 10 Enterprise 1709           10.0.16299.371 

@not2qubit 真的吗?在我的 Surface Book 2 上大约 1 秒。
获取计算机信息 |选择 WindowsVersion 不再适用于 21h1 - 它显示 2009(与 20h2 相同)
这很烦人......微软是如此......一致。不是。
m
mikemaccana

与上述所有解决方案不同,这将为您提供完整版本的 Windows(包括修订/内部版本号):

(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion

结果:

10.0.10240.16392 (th1_st1.150716-1608)

就我而言,这是最好的解决方案,因为它可以正确报告修订号。其他都不是(至少我已经测试过了)。
这是迄今为止唯一能让我获得完整版本号的解决方案。 然而,不是 system32 中的所有文件都会随着每次更新而更新 - 例如,我的 hal.dll 仍然显示 10.0.10586.0 (th2_release.151029-1700),而 winload.exe 有 10.0.10586.63 (th2_release.160104-1513)
这是一个从 dll/exe 中获取具有最高构建日期的版本的小脚本:gist
这依赖于微软方面的实施细节,不能保证他们会继续这样做。它现在可以工作,但如果你希望你的脚本长期工作,你应该避免依赖它。
@Jaykul 好吧,我不同意,有两个原因。 (1)因为那些“1803”之类的数字并不总是可用(例如在Win8上),那么应该在那里使用什么? (2) 没有技术原因为什么应该只有一个正确的version。操作系统是按部分构建(和更新)的,即内核、HAL、UBR 和 功能 等。所以我们应该真正显示所有这些。在这方面,我认为 BuildLabExKernelHAL(按此顺序)将是提供更合适的 版本 的最合适方式。但既然你似乎知道什么是,你应该发布什么是正确
S
Schadowy

从 PowerShell 5 开始:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*

我认为这个命令几乎尝试了迄今为止发现的 1001 种不同的方法来收集系统信息......


我从中得到的部分响应很奇怪......我使用的是 Windows 10 1909,但“WindowsCurrentVersion”是 6.3。我认为那将是 10,因为 6.3 是 Windows 8.1。否则,我喜欢这个命令提供的信息
P
Peter Mortensen

如果要区分 Windows 8.1 (6.3.9600) 和 Windows 8 (6.2.9200),请使用

(Get-CimInstance Win32_OperatingSystem).Version 

获得正确的版本。 [Environment]::OSVersion 在 Windows 8.1 中无法正常工作(它返回 Windows 8 版本)。


请注意,[Environment]::OSVersionwindows-10 中有效,OSVersion.Version.Major 返回 10。
(Get-CimInstance Win32_OperatingSystem).Version[Environment]::OSVersion 都适用于我并返回相同的结果:6.3.9600.0
不幸的是,6.3.9600 不仅仅是 Win 8.1,服务器 2012 R2 也返回相同的内部版本号。
F
Fares

我正在提炼其中一个answers

我在尝试匹配 winver.exe 的输出时遇到了这个问题:

Version 1607 (OS Build 14393.351)

我能够使用以下方法提取构建字符串:

,((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx -split '\.') | % {  $_[0..1] -join '.' }  

结果:14393.351

更新:这是一个使用正则表达式的稍微简化的脚本

(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' |  % { $matches.Values }

M
Michael Joyce

如果您试图破译 MS 放在他们的补丁站点上的信息,例如 https://technet.microsoft.com/en-us/library/security/ms17-010.aspx

您将需要一个组合,例如:

$name=(Get-WmiObject Win32_OperatingSystem).caption $bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture $ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId Write-Host $name, $bit, $ver

微软 Windows 10 家庭版 64 位 1703


R
Ron MVP

我采用了上面的脚本并对其进行了一些调整以得出以下结论:

$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture

$vert = " Version:"
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId

$buildt = " Build:"
$build= (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' |  % { $matches.Values }

$installd = Get-ComputerInfo -Property WindowsInstallDateFromRegistry

Write-host $installd
Write-Host $name, $bit, $vert, $ver, `enter code here`$buildt, $build, $installd

要得到这样的结果:

Microsoft Windows 10 家庭 64 位版本:1709 内部版本:16299.431 @{WindowsInstallDateFromRegistry=18-01-01 2:29:11 AM}

提示:我会很感激从安装日期中删除前缀文本,以便我可以用更易读的标题替换它。


install date 命令需要一段时间才能运行,所以我找到了一个更快的命令:[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970')).AddSeconds($(get-itemproperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").InstallDate) 它稍微复杂一些,但运行起来要快得多。您甚至可以省略时区部分:([datetime]'1/1/1970').AddSeconds($(get-itemproperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").InstallDate)
J
James Russell

要在 Windows 10 1809 上的 PowerShell v5 中生成与 winver.exe 相同的输出:

$Version = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
"Version $($Version.ReleaseId) (OS Build $($Version.CurrentBuildNumber).$($Version.UBR))"

它还与 Windows 10 中“设置 > 系统 > 关于”中的版本相匹配。并获得正确的更新构建修订,这在我的机器上没有许多解决方案 👍
这停止为我工作。 WinVer.exe 显示 Version 20H2 (OS Build 19042.804),但这给出了 Version 2009 (OS Build 19042.804)。将 ReleaseId 更改为 DisplayVersion 可以解决此问题,但旧操作系统没有 DisplayVersion
releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在
m
mhu

正如 MoonStom 所说,[Environment]::OSVersion 在升级的 Windows 8.1 上无法正常工作(它返回 Windows 8 版本):link

如果您想区分 Windows 8.1 (6.3.9600) 和 Windows 8 (6.2.9200),您可以使用 (Get-CimInstance Win32_OperatingSystem).Version 来获取正确的版本。但是,这在 PowerShell 2 中不起作用。所以使用这个:

$version = $null
try {
    $version = (Get-CimInstance Win32_OperatingSystem).Version
}
catch {
    $version = [System.Environment]::OSVersion.Version | % {"{0}.{1}.{2}" -f $_.Major,$_.Minor,$_.Build}
}

P
Peter Mortensen

利用:

Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption

也可以使用这个获取版本号:Get-WmiObject -class win32_operatingsystem |选择版本
您可以通过显示输出来改进此答案。
B
Bob

应该像这样容易:

Get-ComputerInfo  | select windowsversion

获取计算机信息 |选择 WindowsVersion 不再适用于 21h1 - 它显示 2009(与 20h2 相同)
我需要添加,使用 Powershell 执行此命令,谢谢
P
Porky

这确实是一个很长的话题,可能是因为答案虽然正确,但并没有解决基本问题。我偶然发现了这个网站:Version & Build Numbers,它清楚地概述了 Microsoft Windows 世界的内容。

因为我的兴趣是知道我正在处理哪个确切的 Windows 操作系统,所以我将整个版本的彩虹放在一边,而是专注于 BuildNumber。可以通过以下方式获得内部版本号:

([Environment]::OSVersion.Version).Build

或通过:

(Get-CimInstance Win32_OperatingSystem).buildNumber

选择是您的选择,无论您喜欢哪种方式。所以从那里我可以做一些事情:

    switch ((Get-CimInstance Win32_OperatingSystem).BuildNumber) 
{
    6001 {$OS = "W2K8"}
    7600 {$OS = "W2K8R2"}
    7601 {$OS = "W2K8R2SP1"}    
    9200 {$OS = "W2K12"}
    9600 {$OS = "W2K12R2"}
    14393 {$OS = "W2K16v1607"}
    16229 {$OS = "W2K16v1709"}
    default { $OS = "Not Listed"}

}
Write-Host "Server system: $OS" -foregroundcolor Green

注意:如您所见,我将上述内容仅用于服务器系统,但它可以轻松应用于工作站,甚至可以巧妙地扩展以支持两者......但我将把它留给你。

享受,玩得开心!


我很乐意将它应用于工作站,但 PS 如何检测它是在 Windows Server 还是简单的 Windows PC 上运行?
Windows 客户端操作系统版本具有不同的构建值,请查看此 Microsoft 链接:docs.microsoft.com/en-us/windows/win32/sysinfo/…。此外,您需要检查产品类型:(Get-CimInstance Win32_OperatingSystem).productType,如果它等于 1,那么它是一个客户端
The link you gave 没有给出构建值,但 ""+[Environment]::OSVersion.Version.Major+"."+[Environment]::OSVersion.Version.Minor 您有包含 Microsoft 官方构建表的链接吗?
@SebMa 请参阅 Windows 10 release information,在网页底部有每个已发布版本的构建表以及每个版本的构建号。这是Windows 11 release information
N
Nathan Tuggy

Windows PowerShell 2.0:

$windows = New-Object -Type PSObject |
           Add-Member -MemberType NoteProperty -Name Caption -Value (Get-WmiObject -Class Win32_OperatingSystem).Caption -PassThru |
           Add-Member -MemberType NoteProperty -Name Version -Value [Environment]::OSVersion.Version                     -PassThru

Windows PowerShell 3.0:

$windows = [PSCustomObject]@{
    Caption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
    Version = [Environment]::OSVersion.Version
}

对于显示(两个版本):

"{0}  ({1})" -f $windows.Caption, $windows.Version 

g
gtxtreme

你们太努力了。这适用于使用 Enter-PSSession 的本地或远程会话 - 试一试。

您所要做的就是输入:

cmd ?

Microsoft Windows [版本 10.0.19042.1237]


这不仅显示版本号 - 它还启动了一个全新的 shell 实例,而且该实例甚至不是 Powershell!所以请注意,如果您打算以交互方式或在脚本中使用它,而不仅仅是作为 winver 的替代品。
E
Eddie Kumar

除了其他答案之外,这里还有一些可以使用 PowerShell 检索的有用信息:

通过 PowerShell 查询操作系统和硬件信息:

查询通用操作系统(操作系统)信息:

查看操作系统名称的最快方法:

cmd ?

#使用获取计算机信息:

Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

#使用Get-WmiObject:

$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
Write-Host " OS-Name: `t $name `n Architct: `t $bit  `n Release: `t $ver" 

列出主要次要版本信息:

[System.Environment]::OSVersion.Version 

查询主机名:

$Env:ComputerName

或者

hostname    #cmd command

此外,如果您知道 IP 地址,请使用“ping”命令(例如:ping /a <your_ip_address>),您将在第一行看到您的“主机名”。

查询当前(登录)用户:

whoami    #cmd command

或者

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name 

查询映射驱动器:列出映射驱动器 - 使用 WMI:

Get-WmiObject -Class Win32_LogicalDisk | Format-Table 

或者

wmic logicaldisk get name       #list just logical-drive letters

或者,列出逻辑驱动器信息:FreeSpace、Provider(真实网络位置)、Size 和 VolumeName:

wmic logicaldisk list brief

列出映射驱动器 - 使用 [DriveInfo] 类:

[System.IO.DriveInfo]::GetDrives()

列出可移动驱动器:

$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if ($r) {
    return @($r)[-1]
}

查询磁盘容量、空间和卷类型

Invoke-Command -ComputerName S1 {Get-PSDrive C} | Select-Object PSComputerName,Used,Free 

可用空间:

(Get-PSDrive C).Free

或(以 GB 为单位)

[Math]::Floor(((Get-PSDrive C).Free /[Math]::Pow(2, 30)*10)) /10

已用空间:

(Get-PSDrive C).Used

或(以 GB 为单位的已用空间)

[Math]::Floor(((Get-PSDrive C).Used /[Math]::Pow(2, 30)*10)) /10

另外要查看总空间:(以 GB 为单位)

$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/(1024*1024*1024)
OR
$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/[Math]::Pow(2, 30)

四舍五入的值:

[Math]::Floor($totalSpace*10) / 10
OR
[Math]::Round($totalSpace,1)

查询主板信息:

wmic baseboard get product,Manufacturer,version,serialnumber

查询磁盘卷(磁盘分区)信息:Get-Volume 返回有关存储驱动器分区的信息,例如:

Get-Volume                 # All partitions
Get-Volume -DriveLetter C  # Specific partition

#文件系统类型:

Get-Volume -DriveLetter C | select FileSystem
(Get-Volume -DriveLetter C).FileSystem

#分区大小:

Get-Volume -DriveLetter C | select Size
OR (in GB)
[Math]::Floor(((Get-Volume -DriveLetter C).Size/[Math]::Pow(2, 30)*10)) /10

查询内存/查询 RAM

Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum
OR (in GB)
$memory = (Get-WmiObject Win32_PhysicalMemory | Measure -Property Capacity -Sum).Sum
$memory = [Math]::Floor(($memory/[Math]::Pow(2, 30)*10)) /10
$memory.ToString() + " gb"

#Query RAM,包括频率/速度:

Get-CimInstance win32_physicalmemory | Format-Table Manufacturer,Banklabel,Configuredclockspeed,Devicelocator,Capacity,Serialnumber –autosize

如前所述,这个答案有点超出了所提出的问题,但对于那些想要使用 PowerShell 获得更多操作系统或硬件信息的人来说可能很有用。


[System.Environment]::OSVersion.Version 是我最喜欢的,适用于在 mac Monterey 上与 brew 一起安装的 pwsh
谢谢@Timo,我会将其添加到我的答案中以供参考(并不是说它是一个完整的选项列表,但您的输入很有价值,所以谢谢)。
n
not2qubit
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx

尽管已应用服务更新,但仍显示 Windows 11 的 RTM 版本。因此,至少它可能总是与 Windows 11 上的实际版本不同。
J
John Smith
(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64').version

根据 Tim 之前的回答,这个特定位置的好处是该属性已经处于我所说的首选格式。


n
ndemou

相当于 winver 的 Powershell

适用于 20h2 之前的所有 Windows 10 版本,速度快且不太复杂*

function Get-WinVer() {
    $win_release = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").displayversion
    if (!($win_release)) {
        $win_release = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId}
    $win_release
}
Get-WinVer

它准确地显示了 winver.exe 在“版本”旁边显示的内容。

我没想到要读这么多才能得出这段代码,我真的希望我不必为 22h1 调整它(或者当时的名字是什么)。

*:微软确实让它变得比它应该的更复杂


P
Peter Mortensen

与所有其他解决方案(在 Windows 10 上测试)不同,这将为您提供 Windows 的完整和正确(与您在运行 winver.exe 时找到的相同版本号)版本(包括修订/内部版本号):

Function Get-OSVersion {
Param($ComputerName)
    Invoke-Command -ComputerName $ComputerName -ScriptBlock {
        $all = @()
        (Get-Childitem c:\windows\system32) | ? Length | Foreach {

            $all += (Get-ItemProperty -Path $_.FullName).VersionInfo.Productversion
        }
        $version = [System.Environment]::OSVersion.Version
        $osversion = "$($version.major).0.$($version.build)"
        $minor = @()
        $all | ? {$_ -like "$osversion*"} | Foreach {
            $minor += [int]($_ -replace".*\.")
        }
        $minor = $minor | sort | Select -Last 1

        return "$osversion.$minor"
    }
}

我在使用 'localhost' 运行它并在我的 localhost 上使用实际计算机名称(由 'hostname' 返回)时出错 - 是否可以调整此解决方案以允许它从本地计算机获取信息而无需启用服务等?
[xxxxxx] 连接到远程服务器 xxxxxx 失败并显示以下错误消息:客户端无法连接到请求中指定的目标。验证目标上的服务是否正在运行并且正在接受请求。请查阅在目标(最常见的是 IIS 或 WinRM)上运行的 WS-Management 服务的日志和文档。如果目标是 WinRM 服务,在目标上运行以下命令来分析和配置 WinRM 服务:“winrm quickconfig”。了解更多信息,[...]
为我工作。赞成。如果它包含 Windows 10 版本 ID - 1507、1511、1607 等,那将是一个完美的脚本。
A
Ali

我搜索了很多以找出确切的版本,因为 WSUS 服务器显示错误的版本。最好的方法是从 UBR 注册表 KEY 中获取修订。

    $WinVer = New-Object –TypeName PSObject
$WinVer | Add-Member –MemberType NoteProperty –Name Major –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentMajorVersionNumber).CurrentMajorVersionNumber
$WinVer | Add-Member –MemberType NoteProperty –Name Minor –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentMinorVersionNumber).CurrentMinorVersionNumber
$WinVer | Add-Member –MemberType NoteProperty –Name Build –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentBuild).CurrentBuild
$WinVer | Add-Member –MemberType NoteProperty –Name Revision –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' UBR).UBR
$WinVer

l
lig

使用Windows Powershell,可以通过以下方式获取你需要的数据

标题:

(Get-WmiObject -class Win32_OperatingSystem).Caption

发布 ID:

(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId

版本:

(Get-CimInstance Win32_OperatingSystem).version

releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在
P
Prime Nobel

[解决了]

#copy all the code below:
#save file as .ps1 run and see the magic

 Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
 (Get-CimInstance Win32_OperatingSystem).version


#-------------comment-------------#
#-----finding windows version-----#

$version= (Get-CimInstance Win32_OperatingSystem).version
$length= $version.Length
$index= $version.IndexOf(".")
[int]$windows= $version.Remove($index,$length-2)  
$windows
#-----------end------------------#
#-----------comment-----------------#

欢迎来到 SO!当您回答问题时,请尝试解释一下。在这种情况下,还有 20 多个回复,因此请考虑公开您的优点。
K
KERR

您也可以通过检查 OSVersion.Version.Major 来使用类似的东西:

IF ([System.Environment]::OSVersion.Version.Major -ge 10) {Write-Host "Windows 10 or above"}
IF ([System.Environment]::OSVersion.Version.Major -lt 10) {Write-Host "Windows 8.1 or below"}

T
Tim

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64\Version '对于 Win 10 客户端'

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Server.OS.amd64\Version '对于服务器操作系统'


嗨,蒂姆,您能解释一下这有助于找到问题的答案吗?我碰巧现在这些是 Windows 注册表地址,但不是每个人都会。此外,还询问了如何在 PowerShell 中执行此操作。您可以在 PowerShell 中添加代码来说明如何执行此操作吗?
D
DarkSideKillaz

我只想完成一个小脚本。我们使用了之前回答过的switch版本,只是详细说明了。没有地方会给你我们习惯的友好名称。 Windows 10 1909 或 Windows 10 20H2。所以我们必须手动对它们进行编程。

$osversion = (Get-CimInstance -class Win32_OperatingSystem).Caption
$buildnumber = (Get-CimInstance Win32_OperatingSystem).BuildNumber
if($osversion -match "Windows 10")
{   
    switch ($buildnumber) 
    { 
        10240 {$OS = "Windows 10 1507"}
        10586 {$OS = "Windows 10 1511"}
        14393 {$OS = "Windows 10 1607"}
        15063 {$OS = "Windows 10 1703"}
        16299 {$OS = "Windows 10 1709"}
        17134 {$OS = "Windows 10 1803"}
        17763 {$OS = "Windows 10 1809"}
        18362 {$OS = "Windows 10 1903"}
        18363 {$OS = "Windows 10 1909"}
        19041 {$OS = "Windows 10 20H1"}
        19042 {$OS = "Windows 10 20H2"}
        19043 {$OS = "Windows 10 21H1"}
        default { $OS = "Not Listed"}
    }
}
if($osversion -match "Windows Server")
{
    switch ($buildnumber) 
    {
        3790 {$OS = "Windows Server 2003 R2"}
        6001 {$OS = "Windows Server 2008"}
        7600 {$OS = "Windows Server 2008 SP1"}
        7601 {$OS = "Windows Server 2008 R2"}    
        9200 {$OS = "Windows Server 2012"}
        9600 {$OS = "Windows Server 2012 R2"}
        14393 {$OS = "Windows Server 2016"}
        17763 {$OS = "Windows Server 2019"}
    }
}
Write-Host "Server system: $OS | $osversion | $buildnumber" -foregroundcolor Green

现在,如果您想像我想使用 invoke-commandnew-pssession 一样一次扫描多台电脑,请注意 Get-WMIObject 已贬值并替换为 get-ciminstance

如果您想要一个示例,我可以稍后提供 如果您使用的是 windows 2003 R2 或更早版本.. 停止移动到新的操作系统。


N
Newbee

powershell 中的 C:\ 提示符或 cmd 提示符窗口中的 systeminfo 提供操作系统名称版本配置制造商等等...


这已经在其他答案中提到过。
这是否适用于 MS Windows 10?
G
GraceSolutions
$OSVersion = [Version](Get-ItemProperty -Path "$($Env:Windir)\System32\hal.dll" -ErrorAction SilentlyContinue).VersionInfo.FileVersion.Split()[0]

在 Windows 10 上返回:10.0.10586.420

然后,您可以使用该变量访问属性以进行细粒度比较

$OSVersion.Major equals 10
$OSVersion.Minor equals 0
$OSVersion.Build equals 10586
$OSVersion.Revision equals 420

此外,您可以使用以下方法比较操作系统版本

If ([Version]$OSVersion -ge [Version]"6.1")
   {
       #Do Something
   }