Monday, August 18, 2008

How to detect Windows Vista in batch file or logon script

Previously, in our logon scripts, we'd used the 'ver' command to get the windows version, and selected the third word (XP or 2000 etc) but now that no longer works properly, so a better way of doing it is this:

VER | findstr /i "5.0." > nul IF %ERRORLEVEL% EQU 0 set version=2000

VER | findstr /i "5.1." > nul IF %ERRORLEVEL% EQU 0 set version=XP

VER | findstr /i "5.2." > nul IF %ERRORLEVEL% EQU 0 set version=2003

VER | findstr /i "6.0." > nul IF %ERRORLEVEL% EQU 0 set version=Vista

This searches for the build number in the ver string.

8 comments:

Anonymous said...

My XP findstr default setting seemed to be regular expression search because it got match when it compared "[Version 5.1.2600]" and "6.0.". I got better results with "findstr /il".

Edwin said...

You are very close, but you forgot to escape your special characters. The . stands for "any character" unless you prefix it with \

VER | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 set version=2000

VER | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 set version=XP

VER | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 set version=2003

VER | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 set version=Vista

Adam said...

Thanks very much for this! I worked with it a big and made some improvements. This corrects a bug with the script not detecting Windows 2000 properly (because Windows 2000 reports its version number as 5.00.xxxx, not 5.0.xxxx) and it also detects Windows 7. In addition, it initially sets the "Version" environment variable to "Unknown" so that if an OS doesn't get detected, a script won't break trying to read an undefined environment variable. Here's the updated script:

SET Version=Unknown

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

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

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

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

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

Pete said...

One comment to Adam's version. Is "6.1" really Windows 7? My Server 2008 system shows "Version 6.1.7600", which leads me to believe that 6.1 is 2008, and 7.x is Windows 7.

Cameron said...

Anyone know how to detect Vista 32 bit installs versus Cista 64 bit installs?

Anonymous said...

Windows 7 and Windows 2008 R2 are based on the same kernel, so both are version 6.1.

Anonymous said...

THANKS SO MUCH ADAM !

Anonymous said...

You can detect x86 and x64 using this:

:: Start code here...
IF DEFINED ProgramFiles(x86) GOTO IS_x64

:IS_x86
:: This is the 32-bit (i.e. x86) version of windows stuff to run...


:IS_x64
:: This is the 64-bit (i.e. x64) version of windows stuff to run...

:: END CODE HERE...


The ProgramFiles(x86) environment variable only exists on x64 platforms, so it serves as an easy way to determine which platform you are on. I've seen some other code that checks for the Program Files folder or the SysWoW64 Reg keys, but any miswritten app install could put those there inadvertantly. The environment variable check is more stable 'cause pretty much no writes it, only reads it.