Basics/Internet 
CDs and DVDs
Computer Memory Definitions
Electronic Mail
File Extensions
Firewall
General Computer Tips
Internet Tips
Mouse
Search Engine 
Ask Search Engine
Bing Search Engine
Dogpile Search
Google
Internet Search
Yahoo!
Sites to Browse
Wireless Internet
Windows 
Windows 7
Windows Vista
Windows XP
Windows 2000
Windows 98
Windows 95
Windows and DOS / Batch Files
Digital Cameras 
Buying
Accessories
Batteries
Camera Phone
Memory
Digital Photography 
Aerial Photography
Autumn Foliage Photography
Fireworks Photography
Night Photography
Parade Photography
Photography - Misc
Photography - Ideas/Experimentation
Photography - Troubleshooting
Photography - Weather Issues
Portrait Photography
Sunrise and Sunset Photography
Travel Photography
Urban and Cityscape Photography
Waterfall Photography
Wildlife and Animal Photography
Photo Archive - Backup
Photo Printing
Photo Processing
Selling Digital Photos
Web Browsers 
Google Chrome
Internet Explorer 
Internet Explorer 6
Internet Explorer 7
Internet Explorer 8
Internet Explorer 9 
Mozilla Firefox
Opera
Safari for Windows
Gadgets 
Cell Phone
Digital Audio Player
Digital Cameras
iPhone / iPod Touch
Printer
Router
Scanner
Software 
Calculators
Clipboard Managers - Enhancers
Digital Image Resizers
Disk Usage Utilities
Docks - Program Launchers
Duplicate File Removers
File and Folder Search
File Recovery - Undelete
File Renamers
File Splitters
Font Managers - Viewers
Internet Related Software
Legacy Applications
Microsoft Office
Miscellaneous Software
Multimedia Players
Password Managers
RSS Readers
Screen Capture
Sticky Notes
System Information Software
Text Editors - Notepad Replacements
Zip and Unzip Software
MS Office 
Microsoft Office 2010 
Microsoft Excel 2010
Microsoft Outlook 2010
Microsoft PowerPoint 2010
Microsoft Publisher 2010
Microsoft Visio 2010
Microsoft Word 2010
Microsoft Office 2007 
Microsoft Excel 2007
Microsoft Outlook 2007
Microsoft PowerPoint 2007
Microsoft Publisher 2007
Microsoft Visio 2007
Microsoft Word 2007
Microsoft Office 2003 
Microsoft Access 2003
Microsoft Excel 2003
Microsoft Outlook 2003
Microsoft PowerPoint 2003
Microsoft Word 2003
Microsoft Word XP, 97, 2000
Spyware 
Ad-Aware
AntiVir AntiVirus
CounterSpy
HijackThis
Malwarebytes Anti-Malware
McAfee AntiSpyware
Misc Spyware and Adware Scanners
Spy Sweeper
Spybot Search and Destroy
Spyware and Adware - General
Spyware Doctor
SpywareBlaster
Trend Micro Anti-Spyware
Windows Defender
 
"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.

Return to the Windows XP and DOS page.

Print this tip



Get the Newsletter


DISQUS seems to be taking longer than usual. Reload?
blog comments powered by Disqus 

Thanks For Sharing!
Share on facebookShare on twitterShare on emailShare on printShare on gmailShare on stumbleuponShare on favoritesShare on bloggerMore Sharing Services


Newest Tips:
 Google 
Look Inside Google's Data Centers  Sites to Browse - Humor - Pranks 
Make the Internet Safe for Cats  Google 
Find Google Blogs, Plus Google on Twitter, FaceBook, Elsewhere  Microsoft Excel 2010 
Stop Accidentally Showing the Ribbon Shortcut Keys Follow Us!
Email Twitter RSS About MalekTips and the Author 
The MalekTips website was created in 1998 by Andrew Malek of Envision Programming. The page's goal is to freely disperse computer-related tips, hints, and informative articles. Tips are organized to be easy to find, and are presented clearly, in easy-to-understand language. MalekTips also provides information and links to public-domain, open source, freeware, shareware, and commercial software available for download. < more > 
 About Us / Contact Us

Advertising

Computer / Tech Links

Contribute a Tip
Free Computer Help Newsletter!

Link To Us

Privacy Policy

RSS Feeds
Sign Up For Our Free Newsletter!
   
This page and its contents are copyright ©1997 - 2012 Envision Programming. ( copyright/disclaimer )