How to find the Windows version from the PowerShell command line

前端 未结 25 987
野的像风
野的像风 2020-12-22 15:51

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

I\'m using PowerShell 2.0 and tried:

PS C:\\> ver
The term \'ver\' is not recognized as the name o         


        
相关标签:
25条回答
  • 2020-12-22 16:11

    This is really a long thread, and probably because the answers albeit correct are not resolving the fundamental question. I came across this site: Version & Build Numbers that provided a clear overview of what is what in the Microsoft Windows world.

    Since my interest is to know which exact windows OS I am dealing with, I left aside the entire version rainbow and instead focused on the BuildNumber. The build number may be attained either by:

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

    or by:

    (Get-CimInstance Win32_OperatingSystem).buildNumber
    

    the choice is yours which ever way you prefer it. So from there I could do something along the lines of:

        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
    

    Note: As you can see I used the above just for server systems, however it could easily be applied to workstations or even cleverly extended to support both... but I'll leave that to you.

    Enjoy, & have fun!

    0 讨论(0)
  • 2020-12-22 16:12

    If you are trying to decipher info MS puts on their patching site such as https://technet.microsoft.com/en-us/library/security/ms17-010.aspx

    you will need a combo such as:

    $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

    0 讨论(0)
  • 2020-12-22 16:14

    I am refining one of the answers

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

    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

    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 }
    
    0 讨论(0)
  • 2020-12-22 16:15

    Since you have access to the .NET library, you could access the OSVersion property of the System.Environment class to get this information. For the version number, there is the Version property.

    For example,

    PS C:\> [System.Environment]::OSVersion.Version
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    6      1      7601   65536
    

    Details of Windows versions can be found here.

    0 讨论(0)
  • 2020-12-22 16:17

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

    Windows 10 has versions of its own: 1507, 1511, 1607, 1703, etc. This is what winver shows.

    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:

    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:

    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
    
    0 讨论(0)
  • 2020-12-22 16:19
    Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
    

    Or golfed

    gwmi win32_operatingsystem | % caption
    

    Result

    Microsoft Windows 7 Ultimate
    
    0 讨论(0)
提交回复
热议问题