Posts Tagged ‘operating system in batch script’

Script to Find Operating System Version

February 2, 2012 2 comments

The code below will set a variable if the operating system is Windows 2003, Windows 2008, Windows 2008 R2 or Windows 7.  I will add Windows XP and Vista at a later date.


FOR /F “tokens=4-5 delims=, ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”2003” (set OSVER=%%g) ELSE (IF /I “%%g”==”2008” (IF “%%g %%h”==”2008R2” (set OSVER=%%g%%h) ELSE set OSVER=%%g)))

FOR /F “tokens=3 delims= ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”7” set OSVER=%%g)



Since each line of a batch file causes the whole file to be processed again until it reaches the next line, I put as much logic in a single line as possible. That means several nested IF statements where necessary.

The variable %OSVER% is set and can be used to process code in other parts of the script that depend on the operating system version. It is suggested to use another nested IF statement directly after this code that will take the script to the part of your file that runs code based on the operating system version.


These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.