Solved

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

Posted on 2011-09-28
13
579 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
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 68

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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now