Determine which Windows OS version is running

DOS - Windows Batch Files, Tips Add comments

Many DOS commands in the 32-bit versions of Windows are similar but support different parameters, different commands or using different registry key name. Thus, if you wish to write a batch file that can run on different types of machines, it may prove beneficial to determine the version of Windows on which the batch file is running. This way the batch file can execute commands appropriate to the operating system.


The simpliest way to know the OS Version is to run the universal windows DOS command “VER” then using “FIND” on it returned result.

@echo off

ver | find “2003″ > nul
if %ERRORLEVEL% == 0 goto v2003

ver | find “XP” > nul
if %ERRORLEVEL% == 0 goto vxp

ver | find “2000″ > nul
if %ERRORLEVEL% == 0 goto v2000

ver | find “NT” > nul
if %ERRORLEVEL% == 0 goto vnt

echo Unknow OS Version.
goto exit

:v2003
:Run Windows 2003 specific commands here.
goto exit

:vxp
:Run Windows XP specific commands here.
goto exit

:v2000
:Run Windows 2000 specific commands here.
goto exit

:vnt
:Run Windows NT-specific commands here.
goto exit

:exit


You can easily add the support of other versions of Windows as necessary or set an environment variable based on the version of Windows detected.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
  • Michael Klement
    Note that this no longer works from W2K3 on, as any identifying name has been removed from the output of 'ver.exe'.

    For instance, on a Vista system you may get:
    Microsoft Windows [Version 6.0.6002]

    You can, however, extract the major and minor version from the output using 'for /f', which implies the OS edition - but only to a degree: Vista and W2K8, as well as Windows 7 and W2K8 R2 are built on the same kernel, and thus report the same version numbers.
blog comments powered by Disqus
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in