"Offering free computer help, hints, and tips to the Internet populace." Now with over 3,820 tips!

Windows XP and DOS

Batch File to Correctly Determine Operating System Version

SUMMARY: Inside a batch file determine if the operating system is Windows NT, Windows 2000, Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, or Windows 7.


 

Many DOS commands in the different versions of Windows are similar but may support different parameters. Plus, newer versions of Windows may support new commands or retire older ones. 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 7, Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, Windows 2000, or Windows 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. Note that for this batch file to correctly discern between newer versions of Windows Server and consumer versions of Windows, it is more convoluted than batch files you may see elsewhere. I have explained the reasoning below.

1) Open a Notepad window.

2) Copy the following text into Notepad (you may want to access this tip's printed version as some lines wrap):

@echo off

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

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

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

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 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

:warnthenexit
echo Machine undetermined.

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

NOTES:

1. The reasoning for using the SYSTEMINFO command rather than relying on the VER command is because Windows Server 2008 "shares" version numbers with other Windows releases (see Microsoft). Thus relying on a "version number" of 6.0 to detect Windows Vista or 6.1 to detect Windows 7 fails to differentiate a machine from Windows Server 2008 or Windows Server 2008 R2.

2. The creation of %TEMP%\osname.txt is solely because I could not place the results of systeminfo | find "OS Name" directly into the for /f command - it does not like piped commands. You may find an easier way to handle grabbing the information from SYSTEMINFO - if so, please comment.

3. The environment variable %vers% has leading spaces. I could remove these with a longer batch file, but in this case it is not necessary.

4. The batch file detects for SYSTEMINFO as it assumes if it gets beyond the older operating system detections, the running version of Windows is even older and will not have this utility. On Windows 7 64-bit it is still located in the %SystemRoot%\system32 folder - if later versions of Windows become 64-bit only, this batch file may have to be updated.



 

Got something to add? Post a comment!

Everyone is invited to submit comments. Registered users also receive their own visitor profile page with a summary of recent posts.
Register | Login

1. Edwin Roestenburg (02/17/2010 02:01:39)  

A caret (^) in front of the pipe allows you to have a pipe in the for-clause. For example: for /f "delims=: tokens=2" %%i IN ('systeminfo ^| find "OS Name"') DO set vers=%%i
(Note the single quotes to denote a command).

Alternative for systeminfo: reg.exe query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
This is faster than SystemInfo.
For example: for /f "tokens=3*" %%i IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| Find "ProductName"') DO set vers=%%i %%j

2. Vijay (02/17/2010 19:13:39)  

Try this to set a variable with OS version name

set /A vers=systeminfo | find "OS Name"

This seems to work for me.

3. DD (02/17/2010 21:20:27)  

To get the OS caption (version) using systeminfo in a batch file without creating a temporary file, try this:

for /f "tokens=2 delims=," %%a in ('systeminfo.exe /FO CSV') do (
set osver=%%a
)
echo %osver%

4. QSquared (02/19/2010 17:45:44)  

Just use:

systeminfo | find "OS Name" | find "2008"

of course. =)

-Q

5. QSquared (02/19/2010 18:50:33)  

systeminfo | find "OS Name" | find "2008"

only works on a one-ff test whcih is where I need it ATM, making saving a text file un-needed.

However, it would probably be much quicker to perform this query (assuming of course admin privileges for the user running it):

START /WAIT REGEDIT.EXE /E "%Temp%.\%%~n0.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
FOR /F "tokens=2 delims==" %%A IN ('TYPE "%Temp%.\%%~n0.dat" ^| FIND /I "ProductName"') DO SET OSSP=%%~A



=D

PS: Please add FB integration using FConnect.

6. Scott (03/09/2010 13:40:18)  

Running this against the 64 bit version of XP (w/SP2) returns "Machine Undetermined". Running the command ver returns "Microsoft Windows [Version 5.2.3790]". Systeminfo returns the correct information and it gets sent to osname.txt but there is no provision to parse the file osname.txt for any version of XP.
Your name:
 E-mail:
* Home/Blog Page:
Comment:
(Max chars: 5000)

* = optional field

 


Print This Tip

Get E-Mail When New Tips are Online

Return to the Windows XP and DOS page.

 


New in MalekTips:

RSS Feeds- Subscribe!

You want the latest tech tips and tricks in your e-mail Inbox - FREE? Type your e-mail address below and click 'Get Tips!'.
 


( sample / details )
( opt-out instructions )