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

181

我该如何查找我正在使用的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

我该如何做到这一点?


8
如果您正在2019年或之后查看此内容,请忽略标记为正确的答案,直接前往真正正确的答案。不用谢。 - BrainSlugs83
如果您在2023年或之后查看此内容,(gin).OSDisplayVersion适用于Win11,而reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v DisplayVersion适用于Win10 22H2,这是Win10的最新版本。 - RoelDS
这个功能可能对某人有帮助。 - asheroto
34个回答

239

既然您可以访问.NET库,您可以访问System.Environment类的OSVersion属性来获取此信息。有关版本号,可以使用Version属性。

例如:

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

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

可以在此处找到有关Windows版本的详细信息。


4
请注意,[Environment]::OSVersion 可在 [tag:windows-10] 中使用,而 OSVersion.Version.Major 返回的是 10。 - yzorg
7
当我运行 winver 命令时,显示的是版本号 1607。但上面的 PowerShell 命令并没有给出 1607。在 PowerShell 中,我应该从哪里获取这个“1607”版本号呢? - CMCDragonkai
11
@(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId - Anton Krouglov
4
这种方法已在Windows 8.1中被弃用。有关详细信息,请参见此[链接](https://blogs.technet.microsoft.com/heyscriptingguy/2014/04/25/use-powershell-to-find-operating-system-version/)。 - Slogmeister Extraordinaire
2
@SlogmeisterExtraordinaire,命令[System.Environment]::OSVersion并没有被弃用,它所使用的后台方法已经被弃用。新版本的PowerShell正在改变后端行为:https://github.com/PowerShell/PowerShell/issues/2009#issuecomment-288516808 - Randy
显示剩余4条评论

127
  1. 要获取Windows版本号,如Jeff在他的答案中所述,请使用以下代码:

    [Environment]::OSVersion
    
    值得注意的是,结果的类型为[System.Version],因此可以使用以下方法检查Windows 7/Windows Server 2008 R2及更高版本。
    [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)
    

    然而,这不会告诉你它是客户端还是服务器端Windows,也不会告诉你版本的名称。

  2. 使用WMI的Win32_OperatingSystem类(始终是单个实例),例如:

  3. (Get-WmiObject -class Win32_OperatingSystem).Caption
    

    将返回类似于以下内容:

    Microsoft® Windows Server® 2008 Standard


Get-WmiObject的注意事项:有时需要几秒钟才能返回。 - ndemou
[Environment]::OSVersion是不可靠的 https://devblogs.microsoft.com/scripting/use-powershell-to-find-operating-system-version/ - az1d
使用.Caption的好答案...终于有一种方法可以获得可读性,而不必去愚蠢的表格中找出它! - KoZm0kNoT

93
很不幸,大多数其他答案都没有提供关于Windows 10的具体信息。
Windows 10有自己的版本:1507、1511、1607、1703等等。这就是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

另请参阅有关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

3
在桌面上,很容易记住使用winver命令获取系统版本信息,在服务器上则使用systeminfo命令。多年来,让我感到困惑的是没有一种统一的方式来获取这些信息。 - MortenB
2
伟大的链接到实际有用的微软信息。需要注意的是,对于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进行比较。 - not2qubit
1
应该是2020年被接受的答案。嘿,SO团队,我们可以随着时间的推移更改被接受的答案吗?这会是一个“元”问题吗? - Timo
3
releaseid在21H1版本中已不再适用,现在使用displayversion。但是这对于较旧的版本来说并不存在。 - az1d
2
我正在使用Windows 11,认为注册表中的BuildLabEx就是我需要的,但实际上它仍然报告了一年前(RTM?)的构建版本22000.1 + lab字符串,而不是根据winver显示的当前版本22000.675。在CurrentVersion键中没有任何内容将我的当前构建版本号作为完整字符串的一部分报告。只有值UBR将报告正确的整数675,但不以完整构建字符串的形式呈现。 - Jonas

43
Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
返回
WindowsProductName    WindowsVersion OsHardwareAbstractionLayer
------------------    -------------- --------------------------
Windows 10 Enterprise 1709           10.0.16299.371 

@not2qubit 真的吗?我的Surface Book 2只需要大约1秒钟。 - Eric Herlitz
1
Get-ComputerInfo | select WindowsVersion 不再适用于21h1 - 它显示2009(与20h2相同)。 - az1d
真烦人...微软就是这样...一点也不稳定。 - Lars Fosdal

26

这将为您提供完整版本的Windows(包括修订/构建号),而不像上面所有的解决方案:

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

结果:

10.0.10240.16392 (th1_st1.150716-1608)

5
在我看来,这是最佳解决方案,因为它能正确地报告修订版本号,而其他方案则没有(至少在我测试的情况下如此)。 - BStateham
8
到目前为止,这是唯一让我得到完整构建号的解决方案。但是,并不是所有system32中的文件都会在每次更新时得到更新 - 例如,我的hal.dll仍显示为10.0.10586.0 (th2_release.151029-1700),而winload.exe则显示为10.0.10586.63 (th2_release.160104-1513) - melak47
3
这是一个获取具有最高构建日期的dll/exe版本的小脚本:gist - melak47
7
这取决于微软方面的一个实现细节,但不能保证他们会继续这样做。目前可以正常工作,但如果想让你的脚本在长期内工作,应避免依赖此特性。 - Nick
1
@Jaykul,我不同意你的观点,原因有两个。(1)因为那些类似于“1803”的数字并不总是可用的(例如在Win8上),那么应该使用什么呢?(2)没有技术原因要求只有一个正确的“版本”。操作系统是由部分构建(和更新)的,即内核、HAL、UBR和功能等。因此,我们应该真正显示它们所有。在这方面,我认为BuildLabExKernelHAL(按顺序)是提供更适当的版本的最合适方式。但既然你知道什么是错误的,那么你应该发布什么是正确的*。 - not2qubit
显示剩余4条评论

16

自PowerShell 5起:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*

我认为这个命令基本上尝试了迄今发现的1001种不同方式来收集系统信息...


我从中得到的部分响应很奇怪...我使用的是Windows 10 1909,但“WindowsCurrentVersion”是6.3。我认为应该是10,因为6.3是Windows 8.1。除此之外,我喜欢这个命令提供的信息。 - Randy

9
如果你想区分Windows 8.1(6.3.9600)和Windows 8(6.2.9200),请使用以下方法:
(Get-CimInstance Win32_OperatingSystem).Version 

要获取正确的版本,请使用以下命令:[System.Environment]::OSVersion.Version在Windows 8.1中[Environment]::OSVersion不起作用(它返回一个Windows 8版本)。

请注意,[Environment]::OSVersion 在 [tag:windows-10] 中可用,OSVersion.Version.Major 返回 10。 - yzorg
1
(Get-CimInstance Win32_OperatingSystem).Version[Environment]::OSVersion 对我都有效,并返回相同的结果:6.3.9600.0。 - VirtualVDX
不幸的是,6.3.9600 不仅仅是 Windows 8.1 的版本号,Server 2012 R2 也返回了这个相同的构建号。 - bytejunkie

8

我正在完善答案中的一个内容。

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

版本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 }

只是一条注释,这在2019年及以后的操作系统中不起作用,要获取完整的构建(包括REV),因为它不再存储在注册表中。(可悲的是) - DefToneR

8
如果您正在解密MS放在其修补程序站点上的信息,例如https://technet.microsoft.com/zh-cn/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 Microsoft Windows 10 Home 64位版1703

7

除了其他回答之外,以下是一些可以使用PowerShell检索的有用信息:

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

查询通用操作系统信息:

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

cmd ?

#使用 Get-ComputerInfo:

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

OR(已使用的空间,以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"

#查询内存包括频率 / 速度:

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

正如提到的那样,这个答案超出了问题的范围,但对于希望使用 PowerShell 获得更多操作系统或硬件信息的人可能会有用。

1
[System.Environment]::OSVersion.Version 是我最喜欢的代码,可以在安装有 brew 的 Mac Monterey 上运行 pwsh - Timo
1
谢谢@Timo,我会把这个加到我的答案里作为参考(虽然这不是一个全面的选项列表,但你的意见很有价值,所以谢谢)。 - Eddie Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接