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?
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.
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.
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.
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.
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.
These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you Mad Tom Vane).
x.y
in a cmd
consolefor /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k) else (echo %i.%j))
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))
%%
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%
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 106.3
Windows Server 20126.3
Windows 8.1/!\
6.2
Windows 8/!\
6.1
Windows 7/!\
6.0
Windows Vista5.2
Windows XP x645.1
Windows XP5.0
Windows 20004.10
Windows 98
Please also up-vote answers from Agent Gibbs and peterbh as my answer is inspired from their ideas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With