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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7330187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:45:04  来源:igfitidea点击:

How to find the Windows version from the PowerShell command line

windowspowershellpowershell-2.0

提问by jrara

How do I find which Windows version I'm using?

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

I'm using PowerShell 2.0 and tried:

我正在使用 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

How do I do this?

我该怎么做呢?

回答by Jeff Mercado

Since you have access to the .NET library, you could access the OSVersionproperty of the System.Environmentclass to get this information. For the version number, there is the Versionproperty.

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

For example,

例如,

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

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

Details of Windows versions can be found here.

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

回答by Richard

  1. To get the Windows version number, as Jeff notes in his answer, use:

    [Environment]::OSVersion
    

    It is worth noting that the result is of type [System.Version], so it is possible to check for, say, Windows 7/Windows Server 2008 R2 and later with

    [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)
    

    However this will not tell you if it is client or server Windows, nor the name of the version.

  2. Use WMI's Win32_OperatingSystemclass (always single instance), for example:

    (Get-WmiObject -class Win32_OperatingSystem).Caption
    

    will return something like

    Microsoft? Windows Server? 2008 Standard

  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类(总是单实例),例如:

    (Get-WmiObject -class Win32_OperatingSystem).Caption
    

    会返回类似的东西

    微软?视窗服务器?2008年标准

回答by Anton Krouglov

Unfortunately most of the other answers do not provide information specific to Windows 10.

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

Windows 10 has versionsof its own: 1507, 1511, 1607, 1703, etc. This is what winvershows.

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

See also related question on superuser.

另请参阅有关超级用户的相关问题

As for other Windows versions use systeminfo. Powershell wrapper:

至于其他 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 output for the same command:

相同命令的 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

回答by Steven Penny

Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption

Or golfed

或打高尔夫球

gwmi win32_operatingsystem | % caption

Result

结果

Microsoft Windows 7 Ultimate

回答by Ihor Zenich

This will give you the full version of Windows (including Revision/Build number)unlike all the solutions above:

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

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

Result:

结果:

10.0.10240.16392 (th1_st1.150716-1608)

回答by Lars Fosdal

PS C:\> Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

returns

回报

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

回答by Schadowy

Since PowerShell 5:

从 PowerShell 5 开始:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*

I think this command pretty much tries the 1001 different ways so far discovered to collect system information...

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

回答by Fares

I am refining one of the answers

我正在完善其中一个答案

I reached this question while trying to match the output from winver.exe:

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

Version 1607 (OS Build 14393.351)

Version 1607 (OS Build 14393.351)

I was able to extract the build string with:

我能够使用以下命令提取构建字符串:

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

Result: 14393.351

结果: 14393.351

Updated: Here is a slightly simplified script using regex

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

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

回答by MoonStom

If you want to differentiate between Windows 8.1 (6.3.9600) and Windows 8 (6.2.9200) use

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

(Get-CimInstance Win32_OperatingSystem).Version 

to get the proper version. [Environment]::OSVersiondoesn't work properly in Windows 8.1 (it returns a Windows 8 version).

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

回答by Ron MVP

I took the scripts above and tweaked them a little to come up with this:

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

$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


To get a result like this:

要得到这样的结果:

Microsoft Windows 10 Home 64-bit Version: 1709 Build: 16299.431 @{WindowsInstallDateFromRegistry=18-01-01 2:29:11 AM}

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

Hint: I'd appreciate a hand stripping the prefix text from the install date so I can replace it with a more readable header.

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