How to get Windows version from command prompt or from PowerShell

127.3k views Asked by At

Windows version.

But is there a way to get the exact version string using command line output similar to the one mentioned in the image?

The attached is the output of "winver" command from run. PS: I am looking for a batch or PowerShell command.

There are some alternates available to get the Windows version like this PowerShell command:

[System.Environment]::OSVersion
9

There are 9 answers

5
shameer1101 On

The following commands are is going to help you with that. If you need more information, just type in systeminfo:

systeminfo | findstr /B /C:"OS Name" /C:"OS Version"

wmic os get Caption,CSDVersion /value
3
knile On

I found it somewhere, PowerShell:

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

With system information you can only get the build with that value and go to Google to get the respective version.

However, one simple way is by searching the registry on the command line:

REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr REG_SZ
2
Mariano Desanze On

The ver command shows something like this:

> ver

Microsoft Windows [Versión 10.0.17134.228]

But in PowerShell (or Git Bash) you have to call it through the cmd command:

> cmd /c ver
3
acadie_man On

To add to @Bonifacio 's answer:

REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr ReleaseId

Would be even better, because it returns only the ReleaseId value, which you could then pipe to a file. Especially useful if you have several hosts to deal with.

REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr ReleaseId > any_path\%COMPUTERNAME%_OS_Version.txt
2
fritolays On

The reg query way suggested all output a little garbage.

REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr ReleaseId
Output:
ReleaseId    REG_SZ    2009

Using a for loop with tokens will output clean information.

for /f "tokens=3" %i in ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ^| findstr ReleaseId') do echo %i
Output:
2009

The tokens=3 refers to the third word from the original output.

You will need to double the % if running inside a bat file.

You can set the output as a variable by replacing echo %i with set build=%i

Also remember to escape ^ any special characters.

Lastly look at HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion for the string that has the required value. You may need to adjust the token count.

1
Phil On

For what it is worth, I combined a few answers into this powershell function. (I tested this using pwsh 7.2.1).

<#
.SYNOPSIS
    Gets information about the version of windows this session is running on.

.OUTPUTS
    A hashtable with various key/value pairs containing version information.

.EXAMPLE
    PS> $winver = Get-Winver
    PS> $winver
    
    Name                           Value
    ----                           -----
    DisplayVersion                 21H2
    ProductName                    Windows 10 Enterprise
    CurrentBuildNumber             19044
    KeyName                        HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion
    Version                        10.0.19044.0
    VersionString                  Microsoft Windows NT 10.0.19044.0
    OsVersion                      Microsoft Windows NT 10.0.19044.0

    PS> $winver.Version    

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    10     0      19044  0

#>
function Get-Winver {
    $keyName = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion"
    $versionKey = (Get-Item $keyName)
    $displayVersion = $versionKey.GetValue("DisplayVersion")
    $productName = $versionKey.GetValue("ProductName")
    $currentBuildNumber = $versionKey.GetValue("CurrentBuildNumber")

    $osver = [System.Environment]::OSVersion

    $winver = [Ordered]@{
        "DisplayVersion"     = $displayVersion
        "ProductName"        = $productName
        "CurrentBuildNumber" = $currentBuildNumber
        "KeyName"            = $keyName
        "Version"            = $osver.Version
        "VersionString"      = $osver.VersionString
        "OsVersion"          = $osver
    }

    return $winver
}
2
New_Kid On

In cmd you can use - ver

C:\Users\user_user>ver

Microsoft Windows [Version 10.0.19044.2130]

In PowerShell from: How to find the Windows version from the PowerShell command line

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

0
Bill_Stewart On

If an open source tool is acceptable, I wrote a console (command-line) executable that collects system information using various APIs and the registry:

https://github.com/Bill-Stewart/osinfo

The tool reports on and tests for the following attributes:

  • Processor architecture (e.g., AMD64)

  • OS build number (e.g., 20348)

  • OS display version (e.g., 21H2)

  • Is current system a domain controller (DC)?

  • Is current system a domain member (joined to a domain)?

  • Is current system a "Home Edition" OS?

  • The product info value returned from the GetProductInfo API function

  • Product type: Workstation or Server

  • The OS release ID (e.g., 2009)

  • Is the current session a Remote Desktop (RD) session?

  • Is the current system a Remote Desktop Services (RDS) server?

  • The OS version (which is 10.0 for Windows 10/Server 2016/Server 2019/Server 2022/Windows 11)