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

时间:2011-09-07 07:20:07

标签: windows powershell powershell-v2.0

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

我该怎么做?

27 个答案:

答案 0 :(得分:132)

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

例如,

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

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

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

答案 1 :(得分:94)

  1. 要获取Windows版本号,正如Jeff在answer中所述,请使用:

    [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
    

    将返回类似

    的内容
      

    Microsoft®WindowsServer®2008标准

答案 2 :(得分:27)

不幸的是,大多数其他答案都没有提供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

答案 3 :(得分:22)

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

或打高尔夫球

gwmi win32_operatingsystem | % caption

结果

Microsoft Windows 7 Ultimate

答案 4 :(得分:16)

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

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

结果:

10.0.10240.16392 (th1_st1.150716-1608)

答案 5 :(得分:9)

从PowerShell 5开始:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*

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

答案 6 :(得分:8)

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

(Get-CimInstance Win32_OperatingSystem).Version 

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

答案 7 :(得分:8)

我正在完善其中一个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 }

答案 8 :(得分:4)

使用:

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

答案 9 :(得分:4)

正如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}
}

答案 10 :(得分:4)

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

返回

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

答案 11 :(得分:3)

我接受了上面的脚本并对其进行了一些微调,以得出以下结论:

$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 Home 64位版本:1709内部版本:16299.431 @ {WindowsInstallDateFromRegistry = 18-01-01 2:29:11 AM}

提示:我很乐意从安装日期中删除前缀文本,以便可以用可读性更高的标题替换它。

答案 12 :(得分:2)

如果您正在尝试解密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

Microsoft Windows 10 Home 64-bit 1703

答案 13 :(得分:2)

这确实是一个漫长的话题,可能是因为尽管正确的答案仍无法解决基本问题。 我遇到了以下站点: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

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

享受,玩得开心!

答案 14 :(得分:2)

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 

答案 15 :(得分:1)

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

答案 16 :(得分:0)

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

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"
    }
}

答案 17 :(得分:0)

我搜索了很多确切的版本,因为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

答案 18 :(得分:0)

您可以使用python来简化操作(可在所有Windows版本和所有其他平台上使用):

import platform

print(platform.system()) # returns 'Windows', 'Linux' etc.
print(platform.release()) # returns for Windows 10 or Server 2019 '10'

if platform.system() = 'Windows':
    print(platform.win32_ver()) # returns (10, 10.0.17744, SP0, Multiprocessor Free) on windows server 2019

答案 19 :(得分:0)

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

标题:

(Get-WmiObject -class Win32_OperatingSystem).Caption

ReleaseId:

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

版本:

(Get-CimInstance Win32_OperatingSystem).version

答案 20 :(得分:0)

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

localStorage.setItem(key, value);
localStorage.getItem(key);

答案 21 :(得分:0)

[已解决]

#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-----------------#

答案 22 :(得分:0)

您还可以通过检查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"}

答案 23 :(得分:0)

应该很容易:

Get-ComputerInfo  | select windowsversion

答案 24 :(得分:0)

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 '对于服务器操作系统'

答案 25 :(得分:0)

(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64').version

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

答案 26 :(得分:-3)

$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
   }
相关问题