Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
for /f "delims=" %l in ('ver') do set VERSION=%l
if "%VERSION%"=="Microsoft Windows [Version 6.1.7600]" ECHO WIN7
I missed the original post. This is one of those interesting questions.
6.1 identifies Windows 7 or Windows Server 2008 R2. The full set of Windows versions are listed on the Microsoft web site.
http://msdn.microsoft.com/en-us/library/ms724832%28VS.85%29.aspx
I believe service packs change the build number (i.e. 6000 for 2008 R2 or 2700 for Windows 7). So depending on your use case you may need to periodically update your script to differentiate between Windows 7 and Windows 7 SP1.
Add this line to the end of the previous example to identify the major version (i.e. Windows XP or Windows 7) regardless of the installed service packs.
set VERSION=%VERSION:~27,3%
Test with:
echo %VERSION%
6.1
Taking another approach, you can extract it from the registry.
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion
Use this syntax to read registry values from the command line.
reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion
CurrentVersion REG_SZ 6.1
This approach works for determining the Internet Explorer version too...
reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer" /v Version
HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
Version REG_SZ 8.0.7600.16385
OK, I took this ideaand ran with it... 🙂
Here is my batch script:
----
@echo off
::Original Code Source - http://malektips.com/xp_dos_0025p.html
::Modified by Mark A. Kelley
set _SCRATCH-FILE_=%TMP%\__Windoze-Version-Scratch___.txt
systeminfo>%_SCRATCH-FILE_%
type %_SCRATCH-FILE_% | find "2003" > nul
if %ERRORLEVEL% == 0 goto :ver_2003
type %_SCRATCH-FILE_% | find "XP" > nul
if %ERRORLEVEL% == 0 goto :ver_xp
type %_SCRATCH-FILE_% | find "2000" > nul
if %ERRORLEVEL% == 0 goto :ver_2000
type %_SCRATCH-FILE_% | find "NT" > nul
if %ERRORLEVEL% == 0 goto :ver_nt
if not exist %SystemRoot%\system32\systeminfo.exe goto :warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
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_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
if exist %_SCRATCH-FILE_% del %_SCRATCH-FILE_%>nul
---
This works on XP & Win 2003 Server, I don't have any other OS to test it on, so Caveat Emptor! 🙂
The original script returned "Machine undetermined." on my Win 2003 Server.
I fixed this by parsing the output of "systeminfo" instead of "ver"
And while we're at it, Here's another way of doing it:
---
@echo off
for /f "tokens=1-5* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion ^| find /i "REG_SZ") do set WINDOZE_VERSION=%%c
for /f "tokens=1-5* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentBuildNumber ^| find /i "REG_SZ") do set WINDOZE_BUILD=%%c
::Source for version info: http://msdn.microsoft.com/en-us/library/ms724832(VS.85).aspx?ppud=4
set WINDOZE_VERSION_NAME=Unknown
if {"%WINDOZE_VERSION%"}=={"4.0"} set WINDOZE_VERSION_NAME=Windows NT?
if {"%WINDOZE_VERSION%"}=={"5.0"} set WINDOZE_VERSION_NAME=Windows 2000
if {"%WINDOZE_VERSION%"}=={"5.1"} set WINDOZE_VERSION_NAME=Windows XP
if {"%WINDOZE_VERSION%"}=={"5.2"} set WINDOZE_VERSION_NAME=Windows XP 64-Bit Edition or Windows Server 2003 or Windows Server 2003 R2
if {"%WINDOZE_VERSION%"}=={"6.0"} set WINDOZE_VERSION_NAME=Windows Vista or Windows Server 2008
if {"%WINDOZE_VERSION%"}=={"6.1"} set WINDOZE_VERSION_NAME=Windows 7 or Windows Server 2008 R2
echo Windoze Version=%WINDOZE_VERSION%, Build %WINDOZE_BUILD% (%WINDOZE_VERSION_NAME%)
---
I don't like this one as well, because it is far less precise in determining the OS version...