Batch File: Easy Way to Detect Windows OS Version


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!

Trackback

Here's what others have to say about 'Batch File: Easy Way to Detect Windows OS Version':

Detection de l'OS et de l'architecture dans un Batch

[...] Détection de l’OS et de l’architecture dans un Batch Référence : http://www.grimadmin.com/article.php/batchfile-easy-way-to-detect-os-version<span style="font-family: Courier New;">REM Check Windows 7</span> <span [...] [read more]

Tracked on Friday, January 31 2014 @ 06:24 AM EST

How To Detect Screensaver | Information

[...] Reflection Of Sound 1 min ago How To Detect Termite Damage In Walls 1 min agoBatch File: Easy Way to Detect Windows OS Version – The … – I just wanted to post a sample batch file that shows you how to detect the running version [...] [read more]

Tracked on Tuesday, September 06 2016 @ 06:51 AM EDT

How To Detect End Of File In C | Information

[...] 1 WRITE A C PROGRAM TO DRAW LINE BY USING DDA AND BRESENHAM’S ALGORITHM.Batch File: Easy Way to Detect Windows OS Version – The … – I just wanted to post a sample batch file that shows you how to detect the running version [...] [read more]

Tracked on Thursday, September 08 2016 @ 12:41 AM EDT

How To Check Errorlevel In Batch File | ab

[...] containing a filename) check if the file exists and then runs the file and …Batch File: Easy Way to Detect Windows OS Version – The … – I just wanted to post a sample batch file that shows you how to detect the running [...] [read more]

Tracked on Thursday, September 22 2016 @ 02:59 PM EDT

hulkbuster

Saturday, October 29 2011 @ 10:25 AM EDT
Doesn't work at all.

Anonymous User

Friday, November 18 2011 @ 03:25 PM EST
oh yes it works.

the only issue i have is that Vista and Windows 2008 return a version such as 6.0.nnnn

matthieuh

Wednesday, January 09 2013 @ 01:32 PM EST
@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

BatchStillBest

Thursday, December 08 2011 @ 04:31 AM EST
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%

BatchStillBest

Thursday, December 08 2011 @ 06:54 AM EST
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%

Anonymous User

Wednesday, July 18 2012 @ 04:33 AM EDT
How about:

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

Anonymous User

Wednesday, July 18 2012 @ 04:34 AM EDT
Or in script:

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

Anonymous User

Wednesday, July 18 2012 @ 04:43 AM EDT
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%

Anonymous User

Thursday, October 18 2012 @ 03:18 PM EDT
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

Baasha

Tuesday, December 20 2011 @ 04:50 AM EST
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 ... ).

PT-PT

Wednesday, July 18 2012 @ 05:32 AM EDT
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 :)

Anonymous User

Monday, August 27 2012 @ 03:22 PM EDT
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

linkstat

Wednesday, September 19 2012 @ 04:01 PM EDT
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!

Anonymous User

Tuesday, September 25 2012 @ 11:03 PM EDT
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

Anonymous User

Thursday, January 03 2013 @ 05:10 AM EST
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.

Anonymous User

Thursday, January 03 2013 @ 05:33 AM EST
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

Anonymous User

Thursday, January 03 2013 @ 05:35 AM EST
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.

aseitz@pps.net

Thursday, April 04 2013 @ 04:12 PM EDT
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

Anonymous User

Friday, June 13 2014 @ 10:38 AM EDT
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 ?
Advertisement