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.
- Open a Notepad window.
- 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 - Save the file as %WINDIR%\whichvers.bat
- Now, from the command prompt, enter: whichvers This will display which version of Windows you are running.