Mohit
Mohit

Reputation: 11314

Get the major version number of OS using command prompt

I am checking for the OS version using command prompt.

I am using following code to get the version number

systeminfo | findstr /B /c:"OS Version" 

Output

For Windows Server 2003

OS Version:                5.2.3790 Service Pack 2 Build 3790

For Windows 7

OS Version:                6.1.7601 Service Pack 1 Build 7601

But I am not sure how can I have Major version number (First digit of version)

How can I get the major version number OR

If can I check if the version numbers first char is 5 OR 6

Upvotes: 2

Views: 3771

Answers (3)

dbenham
dbenham

Reputation: 130819

I would use the ver command instead of systeminfo. My Windows 7 machine reports:

Microsoft Windows [Version 6.1.7601]

The following batch script snippet will define a variable containing the primary version number:

for /f "tokens=2 delims=[]" %%A in ('ver') do for /f "tokens=2 delims=. " %%B in ("%%A") do set "majorVersion=%%B"

Upvotes: 1

Rob
Rob

Reputation: 45771

You could pipe the output again through FINDSTR, for example:

systeminfo | findstr /B /c:"OS Version" | findstr /c:" 6."

And then check the ERRORLEVEL returned to see if 6. was present in the returned string.

NOTE:

  1. I've put a space at the beginning of the second string to try and ensure that it doesn't match against anything else that might be stuffed into the output from systeminfo on an "OS Version" line
  2. This is really brittle as it relies on the format of the returned text from systeminfo
  3. There's doubtless a better way to do this with the VER command
  4. For a really robust way of doing this that is independent of the format of the text returned by systeminfo/ver, consider writing a small program in C# (Express Edition is free) that looks at the System.Environment.OSVersion property and sets an error level for you as this would be string parse-free (and thus a lot less likely to break)!

Upvotes: 2

Loïc MICHEL
Loïc MICHEL

Reputation: 26140

here is a snippet using ver.exe :

for /f "tokens=4 delims=. " %%i in ('ver') do set majorversion=%%i

Upvotes: 1

Related Questions