Solved

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

Posted on 2011-09-28
13
587 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 69

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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 53

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
edit command in Command Prompt through Windows 10 2 79
robocopy vs xcopy vs copy 8 130
Date variable in batch file 2 44
Windows Updates Batch Files (now with executables!) 32 59
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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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