windowsbatch-fileoperating-system

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?


Solution

  • Have you tried using the wmic commands?

    Try wmic os get version

    This will give you the version number in a command line, then you just need to integrate into the batch file.