Batch File to Determine Operating System Version

Posted On 2007-04-14 by FortyPoundHead
Tags: Tip Tutorial Windows XP
Views: 3289

Many DOS commands in the 32-bit versions of Windows are similar but support different parameters or a few different commands. 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 following batch file will determine whether or not the machine is running Windows 2003, XP, 2000, or NT. It can easily be modified to support other versions of Windows as necessary or to set an environment variable based on the version of Windows detected.

  1. Open a Notepad window.
  2. Copy the following text into Notepad:

    @echo off

    ver | find "2008" > nul
    if %ERRORLEVEL% == 0 goto ver_2008

    ver | find "2012" > nul
    if %ERRORLEVEL% == 0 goto ver_2012

    ver | find "2003" > nul
    if %ERRORLEVEL% == 0 goto ver_2003

    ver | find "XP" > nul
    if %ERRORLEVEL% == 0 goto ver_xp

    ver | find "2000" > nul
    if %ERRORLEVEL% == 0 goto ver_2000

    ver | find "NT" > nul
    if %ERRORLEVEL% == 0 goto ver_nt

    ver | find "Version 6.1" > nul
    if %ERRORLEVEL% == 0 goto ver_Windows7

    echo Machine undetermined.
    goto exit

    :Run Windows 2008-specific commands here.
    echo Windows 2008
    goto exit

    :Run Windows 2012-specific commands here.
    echo Windows 2012
    goto exit

    :Run Windows 2003-specific commands here.
    echo Windows 2003
    goto exit

    :Run Windows XP-specific commands here.
    echo Windows XP
    goto exit

    :Run Windows 2000-specific commands here.
    echo Windows 2000
    goto exit

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

    :Run Windows 7-specific commands here
    echo Windows 7
    goto exit

  3. Save the file as %WINDIR%\whichvers.bat
  4. Now, from the command prompt, enter: whichvers This will display which version of Windows you are running.

About the Author

FortyPoundHead has posted a total of 1974 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links