Home > Scripts > Script to Find Operating System Version

Script to Find Operating System Version

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.

:OSCHK

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)

 

Notes:

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.

Advertisements
  1. July 13, 2014 at 9:19 am

    This is really fascinating, You are an overly professional blogger.
    I have joined your feed and look ahead to in search of more
    of your great post. Additionally, I have shared your web site in my social networks

  2. October 11, 2014 at 4:17 pm

    Πολύ χρήσιμο το άρθρο σας!
    Συγχαρητήρια!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: