Solved

Command Line to check for XP, Windows 7 and run script accordingly

Posted on 2011-09-28
13
595 Views
Last Modified: 2012-08-27
Looking for a way to make the command line look for Windows XP or Windows 7 and run the correct script accordingly.  I want to keep this a batch file and not a vbs script.  Can you have it check the registry for XP and then GOTO WinXP or GOTO Win7?
0
Comment
Question by:puter_geek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 3

Expert Comment

by:_-W-_
ID: 36718955
Your first option is to type "winver"

Second option is to type "systeminfo" for a whole lot of information.

But the first would be the best option.
0
 
LVL 13

Expert Comment

by:Govvy
ID: 36718959
ver command line should help
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36719010
Parsing VER isn't that easy at it seems - you need to translate the parts of the version number into OS releases. E.g. 5.0  is W2000, 5.1 XP, aso. The different ways mentioned above are shown in
http://www.windows-commandline.com/2009/01/find-windows-os-version-from-command.html (for systeminfo) and http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_27227527.html (for VER), and several other question on EE.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Accepted Solution

by:
ReneGe earned 350 total points
ID: 36719208
Here you go

@ECHO OFF

VER | FINDSTR -i /c:"version 5" >NUL && GOTO WXP
VER | FINDSTR -i /c:"version 6" >NUL && GOTO W7

ECHO THE OS IS NEITHER XP OF WINDOWS 7
PAUSE
EXIT

:WXP
ECHO Windows XP
pause
exit

:W7
ECHO Windows 7
pause
exit

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36719298
Another way

@ECHO OFF


FOR /F "delims=" %%A in ('systeminfo') DO (
	ECHO %%A | FINDSTR -i /c:"WINDOWS XP" >NUL && GOTO WXP
	ECHO %%A | FINDSTR -i /c:"WINDOWS 7" >NUL && GOTO W7
)

ECHO THE OS IS NEITHER XP OF WINDOWS 7
PAUSE
EXIT

:WXP
ECHO Windows XP
pause
exit

:W7
ECHO Windows 7
pause
exit

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36719354
Why not another one?

@ECHO OFF


FOR /F "delims=" %%A in ('WMIC OS get caption') DO (
	ECHO %%A | FINDSTR -i /c:"WINDOWS XP" >NUL && GOTO WXP
	ECHO %%A | FINDSTR -i /c:"WINDOWS 7" >NUL && GOTO W7
)

ECHO THE OS IS NEITHER XP OF WINDOWS 7
PAUSE
EXIT

:WXP
ECHO Windows XP
pause
exit

:W7
ECHO Windows 7
pause
exit

Open in new window

0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36719456
If all you are looking to do is differentiate between systems with Windows 7 and earlier systems, then I'd suggest this slightly simplified approach.  If you need to check more versions of windows then let us know that.

ver | findstr /IL " 6.1.7" > NUL && (
  REM put logic for Windows 7 here
) || (
  REM put logic for non Windows 7 here
)

Open in new window

~bp
0
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 150 total points
ID: 36813121
A quick look on Wikipedia lists 2 versions of Win7 (7 and 7SP1) and 2 versions of XP (XP and XP64).

XP        Microsoft Windows XP [Version 5.1.2600]
XP64      Microsoft Windows [Version 5.2.3790]

Win7      Microsoft Windows [Version 6.1.7600]
Win7SP1   Microsoft Windows [Version 6.1.7601]

Open in new window


The only consistency seperating them is the '6.1' (or 6.1.7 as billprew states above) as indicated by the DOS command VER.

This is all very well providing you know you're only testing either XP or Win7. The reason for this is Windows Server 2008 Release 2 has the same version number as Win7.

Win7      Microsoft Windows [Version 6.1.7600]
S2008R2   Microsoft Windows [Version 6.1.7600]

Open in new window


Similarly, Windows Server 2003 is the same version as XP64.

XP64     Microsoft Windows [Version 5.2.3790]
S2003    Microsoft Windows [Version 5.2.3790]

Open in new window


What this all means is that providing your Windows version is either XP or Win7 the easiest method to distinguish between them is to test for the presence of '6.1'. If the version number contains '6.1' then it's Win7 as in the following batch file code:

@echo off

ver | find "6.1" && goto WIN7 || goto WINXP
exit /b


:WIN7
  ::
  :: Your code for Win7
  ::
goto :eof

:WINXP
  ::
  :: Your code for WinXP
  ::
goto :eof

Open in new window


It gets complicated if your batch file has to determine Windows' version if there is a possibility it could be sitting on one the following:

   Server 2008 R2      - Same as Win7

   Server 2003           - Same as XP64

Furthermore, XP and XP64 conatain little similarity ('Version 5.') which is also shared by Server 2003 and Win2K.
0
 
LVL 7

Author Closing Comment

by:puter_geek
ID: 36933166
Splitting the points.  ReneGe had the most of what I was looking for, but Paultomasi had the 64bit versions as well.

Thanks all!!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36933180
Gald I could help!
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36933790
Well done ReneGe....
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36934670
Thanks pal!

Comming from you means a lot!

Cheers
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36934867
No probs...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Remote Application Install 5 87
Pros and Cons of setting up a batch processing server? 8 125
how to count files? 4 84
xcopy using a text file to a folder 13 80
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question