Use a Batch File to Detect Windows 2K, XP, 2003, Vista, or 7

A while ago I was in a situation where I needed to be able to detect the version of Windows that was being used to execute a batch file. After searching the Internet, the best script I found was this one by Rod of Rod.Net. I made some modifications to it and I came up with what I believe is the best script to use to detect which version of Windows is being used as long as it’s 2K, XP, 2003, Vista, or 7. Here’s the script:

@ECHO OFF
SET OSVersion=Unknown

VER | FINDSTR /L "5.0" > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=2000

VER | FINDSTR /L "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=XP

VER | FINDSTR /L "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=2003

VER | FINDSTR /L "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista

VER | FINDSTR /L "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=7

IF %OSVersion%==Unknown (
 ECHO Unable to determine your version of Windows.
) ELSE (
 ECHO You appear to be using Windows %OSVersion%
)

ECHO.
PAUSE
About these ads

4 comments

  1. This is wrong. Windows Vista and Windows Server 2008 reports version as “6.0”. Windows Vista with SP1 reports version as “6.1” as Windows 7.
    You need to use other approach.

    1. Here a corrective which do difference btw 7 / 2008 and Vista SP1

      nb : I kept “VER | FINDSTR /L “X.X” > NUL” for Windows 2000 & XP because Wmic isn’t installed by default.

      @ECHO OFF
      SET OSVersion=Unknown

      VER | FINDSTR /L “5.0” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=2000

      VER | FINDSTR /L “5.1.” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=XP

      VER | FINDSTR /L “5.2.” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=2003

      VER | FINDSTR /L “6.0.” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista

      :: VER | FINDSTR /L “6.1.” > NUL
      :: IF %ERRORLEVEL% EQU 0 SET OSVersion=”7 Or Vista SP1 Or 2008″

      WMIC Os get Caption | FINDSTR “Vista” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista-SP1

      WMIC Os get Caption | FINDSTR “7” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=7

      WMIC Os get Caption | FINDSTR “2008” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=2008

      VER | FINDSTR /L “6.2.” > NUL
      IF %ERRORLEVEL% EQU 0 SET OSVersion=8

      IF %OSVersion%==Unknown (
      ECHO Unable to determine your version of Windows.
      ) ELSE (
      ECHO You appear to be using Windows %OSVersion%
      )

      ECHO.
      PAUSE

      Enjoy :)

  2. Adding Windows 8

    @ECHO OFF
    SET OSVersion=Unknown

    VER | FINDSTR /L “5.0” > NUL
    IF %ERRORLEVEL% EQU 0 SET OSVersion=2000

    VER | FINDSTR /L “5.1.” > NUL
    IF %ERRORLEVEL% EQU 0 SET OSVersion=XP

    VER | FINDSTR /L “5.2.” > NUL
    IF %ERRORLEVEL% EQU 0 SET OSVersion=2003

    VER | FINDSTR /L “6.0.” > NUL
    IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista

    VER | FINDSTR /L “6.1.” > NUL
    IF %ERRORLEVEL% EQU 0 SET OSVersion=7

    VER | FINDSTR /L “6.2.” > NUL
    IF %ERRORLEVEL% EQU 0 SET OSVersion=8

    IF %OSVersion%==Unknown (
    ECHO Unable to determine your version of Windows.
    ) ELSE (
    ECHO You appear to be using Windows %OSVersion%
    )

    ECHO.
    PAUSE

  3. stolen from: http://ss64.com/nt/ver.html

    @Echo off
    Setlocal
    :: Get windows Version numbers
    For /f “tokens=2 delims=[]” %%G in (‘ver’) Do (set _version=%%G)

    For /f “tokens=2,3,4 delims=. ” %%G in (‘echo %_version%’) Do (set _major=%%G& set _minor=%%H& set _build=%%I)

    Echo Major version: %_major% Minor Version: %_minor%.%_build%

    if “%_major%”==”5″ goto sub5
    if “%_major%”==”6″ goto sub6

    Echo unsupported version
    goto:eof

    :sub5
    ::Winxp or 2003
    if “%_minor%”==”2″ goto sub_2003
    Echo Windows XP [%PROCESSOR_ARCHITECTURE%]
    goto:eof

    :sub_2003
    Echo Windows 2003 or XP 64 bit [%PROCESSOR_ARCHITECTURE%]
    goto:eof

    :sub6
    if “%_minor%”==”1″ goto sub7
    Echo Windows Vista or Windows 2008 [%PROCESSOR_ARCHITECTURE%]
    goto:eof

    :sub7
    Echo Windows 7 or Windows 2008 R2 [%PROCESSOR_ARCHITECTURE%]
    goto:eof

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