Batch File: Easy Way to Detect Windows OS Version


Scripts & Tools

I just wanted to post a sample batch file that shows you how to detect the running version of Microsoft's Windows operating system in case you need to run specific commands based on which OS the batch file is run on. Read on for the sample code.

CheckOS.bat

REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_2000
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_XP
ver | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_2003
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_Vista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_Win7
goto warn_and_exit

:ver_Win7
:Run Windows 7 specific commands here
REM echo OS Version: Windows 7 (debug line)
goto end

:ver_Vista
:Run Windows Vista specific commands here
REM echo OS Version: Windows Vista (debug line)
goto end

:ver_2003
:Run Windows Server 2003 specific commands here
REM echo OS Version: Windows Server 2003 (debug line)
goto end

:ver_XP
:Run Windows XP specific commands here
REM echo OS Version: Windows XP (debug line)
goto end

:ver_2000
:Run Windows 2000 specific commands here
REM echo OS Version: Windows 2000 (debug line)
goto end

:warn_and_exit
echo Machine OS cannot be determined.

:end  

 

Tag: batch files operating systems

Share It!

Doesn't work at all.
oh yes it works.

the only issue i have is that Vista and Windows 2008 return a version such as 6.0.nnnn
@echo off
cls
REM Identify OS.
ver | find /i "version 6.2." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 8
ver | find /i "version 6.1." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 7
ver | find /i "version 6.0." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows Vista
ver | find /i "version 5.1." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows XP
ver | find /i "version 5.2." > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 2003
ver | find /i "Windows 2000" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 2000
ver | find /i "Windows NT" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows NT
ver | find /i ">Windows ME" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows ME
ver | find /i "Windows 98" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 98
ver | find /i "Windows 95" > nul
if %errorlevel%==0 set $VERSIONWINDOWS=Windows 95

REM Identify bit
IF NOT EXIST "%SYSTEMDRIVE%\Program Files (x86)" set $VERSIONBIT=32 bit
IF EXIST "%SYSTEMDRIVE%\Program Files (x86)" set $VERSIONBIT=64 bit

REM Display result
echo %$VERSIONWINDOWS% %$VERSIONBIT%
echo.
pause
Try this (Tested on Win2k, 2k3, XP, Server 2k8, 2k8R2, and Win7):



@ECHO off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /f "tokens=1,2,3* delims=." %%a IN ('ver') DO (
SET WVer=%%a
SET WVer=!WVer:~-1!
SET WVer=!WVer!.%%b.%%c
SET WVer=!WVer:]=!
)
ECHO %WVer%
Or even...




@ECHO off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /f "tokens=1,2* delims=." %%a IN ('ver') DO (

SET WVer=%%a
SET WVer=!WVer:~-1!
SET WVer=!WVer!.%%b.%%c
SET WVer=!WVer:]=!
)

IF DEFINED ProgramFiles(x86) (
SET OSBit=x64
) ELSE (
SET OSBit=x86
)

ECHO %WVer% %OSBit%
How about:

FOR /f "tokens=2,3,4 delims=[.]" %a IN ('ver') DO SET WVer=%a.%b.%c
SET WVer=%WVer:Version =%
ECHO.%WVer%
Or in script:

FOR /f "tokens=2,3 delims=[.]" %%a IN ('ver') DO SET WVer=%%a.%%b.%%c
SET WVer=%WVer:Version =%
ECHO.%WVer%
Correction:
Or in script:

FOR /f "tokens=2,3,4 delims=[.]" %%a IN ('ver') DO SET WVer=%%a.%%b.%%c
SET WVer=%WVer:Version =%
ECHO.%WVer%
great script. I added a few lines to EACH section for processor architecture



goto 2003_%PROCESSOR_ARCHITECTURE%



:2003_x86

REM commands here for x86

REM jump over x64

goto 2003_end





:2003_x64

REM commands here for x64

REM exit section as normal

:2003_end

goto end

I want to run different things on Windows 7 and Windows 2008, so I am using this script:

http://malektips.com/xp_dos_0025.html

Except that I suggest to replace the line
systeminfo | find "OS Name" > %TEMP%\osname.txt

with
systeminfo | find "Microsoft Windows" > %TEMP%\osname.txt

to correctly detect os version also on other language editions than english (german, czech, russian and so on ... ).
It was identifying W2k3 R2 as 2008 because they share some numbers.In order to solve it I just added "Version " to the findstr.

ver | findstr /i "Version 5\.1\." > nul
IF %ERRORLEVEL% EQU 0 GOTO WindowsXP_check
ver | findstr /i "Version 6\.1\." > nul
IF %ERRORLEVEL% EQU 0 GOTO Windows2008_check

:WindowsXP_check
echo this is a Windows XP family member

:Windows2008_check
echo this is Windows 7 family member

Thanks for sharing this :)
I checked out that batch file and the comments, and combined them it to come up with this (it works quite well):

@echo off

ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto

warnthenexit

FOR /F "delims=: tokens=2" %%i IN ('systeminfo 2^>NUL ^| find "OS

Name"') DO set vers=%%i

echo %vers% | find "Windows 8" > nul
if %ERRORLEVEL% == 0 goto ver_8

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_8
:Run Windows 8 specific commands here.
echo Windows 8
goto exit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_2008
:Run Windows Server 2008 specific commands here.
echo Windows Server 2008
goto exit

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 2003
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit

:ver_2000
:Run Windows 2000 specific commands here.
echo Windows 2000
goto exit

:ver_nt
:Run Windows NT specific commands here.
echo Windows NT
goto exit

:warnthenexit
echo Machine undetermined.

:exit
Hello. Thanks for "the final script" for easy detect the Windows version. I tried to probe this, but i have one error with the command:
FOR /F "delims=: tokens=2" %%i IN ('systeminfo 2^>NUL ^| FIND "OSName"') DO
The answer is:
No se esperaba %%i en este momento.
(sorry, my Windows is in spanish), but in english something like:
%%i Unexpected at this time
Why? Please, I'm needing some help with this. Greetings!
Just change the code to one line for each command, like this:


if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

FOR /F "delims=: tokens=2" %%i IN ('systeminfo 2^>NUL ^| find "OS Name"') DO set vers=%%i

Buena Suerte
This script didnt work for me, if I type VER into a CMD prompt I get:
Microsoft Windows [Version 5.2.3790]
for a 2003 server. I dont understand how the script is supposed to find "2003" from whats returned by the Ver command.

I also received these errors when running the script:
'warnthenexit' is not recognized as an internal or external command,
operable program or batch file.
| was unexpected at this time.
Okay so the errors were to do with code not being on the same line - fixed
I still get the "error %%i was unexpected" at this time when I run:
FOR /F "delims=: tokens=2" %%i IN ('systeminfo 2^>NUL ^| find "OS Name"') DO set vers=%%i
If you are doing it from the command line, you don't have to escape the %, so %i is sufficient. You only need to use %%i from batch files.
I don't check for the pathing for the 32bit vs 64bit since it is already done by the OS. I just append the variable to the OS version. Here is a sample of three version checks.

@ECHO OFF

REM XP Check
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 SET VER=XP-%PROCESSOR_ARCHITECTURE%

REM 7 Check
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 SET VER=WIN7-%PROCESSOR_ARCHITECTURE%

REM 8 Check
ver | findstr /i "6\.2\." > nul
IF %ERRORLEVEL% EQU 0 SET VER=WIN8-%PROCESSOR_ARCHITECTURE%

ECHO %VER%
PAUSE
I have a notepad with server names in it.
Is there a batch file to return the OS names and service pack types of those servers in a XL sheet ?
To solve more complected issue in window 10 you can choose this web service https://windowsclassroom.com/how-to-fix-error-code-0x80004005-windows-10/
(http://google.com)[hello sir]
[http://google.com][hello sir]

<a href="https://google.com/">google</a>


[http://google.com]
your video [gaming experience](http://google.com) will begin when the pre-paid card is triggered.