Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Windows version in a batch file

I need to get the OS version with a batch file. I 've seen a lot of examples online, many uses something like this code:

@echo off  ver | find "XP" > nul if %ERRORLEVEL% == 0 goto ver_xp  if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit  systeminfo | find "OS Name" > %TEMP%\osname.txt FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i  echo %vers% | find "Windows 7" > nul if %ERRORLEVEL% == 0 goto ver_7  echo %vers% | find "Windows Vista" > nul if %ERRORLEVEL% == 0 goto ver_vista  goto warnthenexit  :ver_7 :Run Windows 7 specific commands here. echo Windows 7 goto exit  :ver_vista :Run Windows Vista specific commands here. echo Windows Vista goto exit  :ver_xp :Run Windows XP specific commands here. echo Windows XP goto exit  :warnthenexit echo Machine undetermined.  :exit 

The problem is when I execute this on Vista or Windows 7 I get the message

Machine undetermined

Is there any other way to do what I want?

like image 722
Yerko Antonio Avatar asked Nov 03 '12 18:11

Yerko Antonio


People also ask

How do I check Windows version in CMD?

Using Command Prompt or PowerShell At the Command Prompt or PowerShell, type "slmgr /dlv", and then press ENTER. The /dlv command displays the detailed licensing information.

What is Ver command in CMD?

In computing, ver (short for version) is a command in various command-line interpreters (shells) such as COMMAND.COM , cmd.exe and 4DOS/4NT. It prints the name and version of the operating system, the command shell, or in some implementations the version of other commands.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

How do I find Windows version in registry?

1 – Press and hold windows logo key and press R on your keyboard to open run. 2 – Now, type regedit in it. 3 – Now, in the registry editor go to the following location from the left side menu. 4 – In the right side all the build number, version number are written.


2 Answers

It's much easier (and faster) to get this information by only parsing the output of ver:

@echo off setlocal for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j if "%version%" == "10.0" echo Windows 10 if "%version%" == "6.3" echo Windows 8.1 if "%version%" == "6.2" echo Windows 8. if "%version%" == "6.1" echo Windows 7. if "%version%" == "6.0" echo Windows Vista. rem etc etc endlocal 

This table on MSDN documents which version number corresponds to which Windows product version (this is where you get the 6.1 means Windows 7 information from).

The only drawback of this technique is that it cannot distinguish between the equivalent server and consumer versions of Windows.

like image 86
Jon Avatar answered Sep 23 '22 21:09

Jon


These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you Mad Tom Vane).

Extract version x.y in a cmd console

for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k) else (echo %i.%j)) 

Extract the full version x.y.z

for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k.%l) else (echo %i.%j.%k)) 

In a batch script use %% instead of single %

@echo off for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (if %%i==Version (set v=%%j.%%k) else (set v=%%i.%%j)) echo %v% 

Version is not always consistent to brand name number

Be aware that the extracted version number does not always corresponds to the Windows name release. See below an extract from the full list of Microsoft Windows versions.

10.0   Windows 10
6.3   Windows Server 2012
6.3   Windows 8.1   /!\
6.2   Windows 8   /!\
6.1   Windows 7   /!\
6.0   Windows Vista
5.2   Windows XP x64
5.1   Windows XP
5.0   Windows 2000
4.10   Windows 98

Please also up-vote answers from Agent Gibbs and peterbh as my answer is inspired from their ideas.

like image 33
oHo Avatar answered Sep 23 '22 21:09

oHo