Batch file to detect Windows version number
Posted by jpluimers on 2012/02/08
Most Batch files for detecting Windows versions try to parse the either the [WayBack] output fromVER
or the [WayBack] output from SYSTEMINFO
[WayBack], but forget that there many Windows installations are not English. Some even use WMIC, but [WayBack] WMIC is only available for administrators and not available some flavours like XP Home.
Languages issues are always important to watch for. The Dutch Windows XP returns Microsoft Windows XP [versie 5.1.2600] which is just one word different from the English Microsoft Windows XP [Version 5.1.2600]. Other languages may differ even more.
This batch file tries to circumvent the language differences, uses VER
and works at least with Dutch and English Windows versions of XP and 7, most likely with many other languages and versions as well.
On a Windows XP SP3 machine, it lists WindowsVersion=5.1.2600
and on a Windows 7 SP1 machine it lists WindowsVersion=6.1.7601
.
One possible addition would be to [WayBack] detect x64 or x86.
The detection assumes that VER will emit the version in [angle] brackets, and uses two batch file for loops to get the text in between them using the [WayBack] tokens and delims for loop parameters in the first for loop right behind the begin label and the second for loop right after the parse1 label.
Then it splits the remaining text using spaces at the parse2 label, and takes the right most portion using the [WayBack] shift command at the parse3 label.
Many thanks to [WayBack] Rob van der Woude for a lot of interesting batch file documentation. The [WayBack] Wikipedia page on VER lists the Windows versions that you can see. This is a summary starting at Windows 2000 [the first using angle brackets]:
- Windows 2000: Microsoft Windows 2000 [Version 5.00.2195]
- Windows XP: Microsoft Windows XP [Version 5.1.2600]
- Windows XP 64bit: Microsoft Windows [Version 5.2.3790]
- Windows Server 2003: Microsoft Windows [Version 5.2.3790]
- Windows Vista: Microsoft Windows [Version 6.0.6001]
- Windows Server 2008: Microsoft Windows [Version 6.0.6002]
- Windows Server 2008 R2: Microsoft Windows [Version 6.1.7600]
- Windows 7: Microsoft Windows [Version 6.1.7600]
- Windows 7 SP1: Microsoft Windows [Version 6.1.7601]
- Windows 8.1: Microsoft Windows [Version 6.3.9600]
- Windows 10 Build 1803: Microsoft Windows [Version 10.0.17134.165]
And here is the batch file:
@echo off :begin :: get everything from "ver" starting after the first [ setlocal enableextensions for /f "tokens=2 delims=[" %%a in ('ver') do (call :parse1 %%a) echo WindowsVersion=%WindowsVersion% endlocal goto :end :parse1 :: strip everything from the last [ ::DEBUG echo %* for /f "tokens=1 delims=]" %%a in ("%*") do (call :parse2 %%a) goto :end :parse2 :: get all ::DEBUG echo %* for /f "tokens=* delims= " %%a in ("%*") do (call :parse3 %%a) goto :end :parse3 ::DEBUG echo %* set WindowsVersion=%1 shift if !!==!%1! goto :WindowsVersion goto :parse3 :WindowsVersion ::DEBUG echo WindowsVersion=%WindowsVersion% goto :end :end
–jeroen
Leave a Reply