Solved

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

Posted on 2011-09-28
13
584 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Robocopy 5 86
Find and Copy Specific Files from a Subfolder Tree 6 51
Need to Copy files and folders in a batch file 2 83
How do I delete 2 42
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

25 Experts available now in Live!

Get 1:1 Help Now