How to Get Windows Version from Command Prompt or from Powershell
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"...
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
7 Answers
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
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
I found it somewhere, PowerShell:
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
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
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
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.
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
}