fortypoundhead.com

Batch File to Determine Operating System Version

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


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

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

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

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

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

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

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

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

    :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:54.158.252.131

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



Recent Forum Posts

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers

New security implemented
dwirch posted on August 8, 2017 at about 6:43 in Site News

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News