The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,229 other subscribers

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 [WayBackoutput from SYSTEMINFO [WayBack], but forget that there many Windows installations are not English. Some even use WMIC, but [WayBackWMIC 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 [WayBacktokens 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 [WayBackshift command at the parse3 label.

Many thanks to [WayBackRob van der Woude for a lot of interesting batch file documentation. The [WayBackWikipedia 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
  :: 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%
goto :end

  :: strip everything from the last [
  ::DEBUG echo %*
  for /f "tokens=1 delims=]" %%a in ("%*") do (call :parse2 %%a)
goto :end

  :: get all
  ::DEBUG echo %*
  for /f "tokens=* delims= " %%a in ("%*") do (call :parse3 %%a)
goto :end

  ::DEBUG echo %*
  set WindowsVersion=%1
  if !!==!%1! goto :WindowsVersion
  goto :parse3
  ::DEBUG echo WindowsVersion=%WindowsVersion%
goto :end



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: