Home > Software engineering >  Windows version from a batch script
Windows version from a batch script

Time:08-07

I need to add to a script that gets the windows version, Ex. 21H2.

I can get the OS name and build, but really need the 21H2.

What I have is:

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

And I get:

OS Name:                   Microsoft Windows 10 Enterprise
OS Version:                10.0.19044 N/A Build 19044

Does anyone know how to get the 21H2, without using PowerShell, (which is disallowed in my work environment).

CodePudding user response:

Regardless of my earlier comment, the simplest, and quickest way to retrieve the result requested in your specific question is:

@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /V DisplayVersion 2^>NUL') Do @Echo %%H

[EDIT /]

BTW, based upon my earlier comment, you can change DisplayVersion to CurrentBuildNumber, if 19044 will still satisfy your requirements. This could be useful because the number can be better used in comparisons with IF and EQU/GTR/GEQ/LSS/LEQ etc.


And if you want the OS, Edition, Version, and Build all in one command, you could probably expand that to something like this:

@For /F "EOL=H Tokens=1,2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /T REG_SZ 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^  *CurrentBuildN" /C:"  *ProductN" /C:"  *D" ^| %SystemRoot%\System32\sort.exe /R') Do @Set /P "=%%I " 0<NUL

Alternatively modify the output:

@For /F "EOL=H Tokens=1,2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /T REG_SZ 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^  *CurrentBuildN" /C:"  *ProductN" /C:"  *D"') Do @Echo %%G: %%I "

Or define three variables:

@For /F "EOL=H Tokens=1,2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /T REG_SZ 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^  *CurrentBuildN" /C:"  *ProductN" /C:"  *D"') Do @Set "%%G=%%I"
  • Related