Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

how to tell what version of windows and/or cmd.exe a batch file is running on?

0 like 0 dislike

How can one determine what version of Windows and/or cmd.exe a batch file is running on?

There is no cmd /version that I've been able to find and the results of SET in a command prompt session don't give anything obviously unique (between XP and Win7 anyway).

asked May 23, 2013 by anonymous  
batch file from MS for How to determine the OS type in a logon script, only covers W95,NT,2000, Enterprise NT
this script from SS64 works from W9x thru XP, but not XPx64 or Win7 x64 -
reg.exe snippet to retrieve version #: {{{ reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion }}}

6 Answers

0 like 0 dislike
Best answer

The version of cmd.exe should actually be pretty irrelevant, unless you try to use features that didn't exist before (in for example). There is the pseudovariable


which holds the version of the command extensions which has been 2 for ages (at least back to NT 4, iirc).

But, back to the point: Running ver and parsing the version string might be your best bet:

for /f "tokens=2 delims=[]" %%x in ('ver') do set WINVER=%%x
set WINVER=%WINVER:Version =%
answered May 29, 2013 by anonymous  
selected May 29, 2013 by sarwana
thank you for pointing me in the right direction and giving me a push start. It was a great help!
0 like 0 dislike

you can use the "systeminfo" @ cmd.exe

C:\Users\Tagon8>systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name:                   Microsoft Windows 8 Release Preview
OS Version:                6.2.8400 N/A Build 8400
answered May 29, 2013 by anonymous  
0 like 0 dislike

I found a shorter way using ver as well:

ver | find "5.1"
if %ERRORLEVEL% == 0 goto ver_winxp

This will find XP, replace the string with your wanted versions

answered May 29, 2013 by anonymous  
0 like 0 dislike

Maybe someone will need the following to determine the SKU (Win7). I'm using some of this script to pick the right OS and XML during sysprep. Hope it helps!

@echo off

set ver=Unknown

systeminfo > C:\sysinfo

findstr /e /c:"Enterprise " C:\sysinfo 1>nul 2>nul    
if %errorlevel% equ 0 set ver=Enterprise

findstr /e /c:"Ultimate " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Ultimate

findstr /e /c:"Professional " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Professional

findstr /e /c:"Home Premium " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Home Premium

findstr /e /c:"Home Basic " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Home Basic    

del /f /q C:\SP\sysinfo 1>nul 2>nul
Echo Windows 7 %ver%

answered May 29, 2013 by anonymous  
Thanks for a usable script, though I'm leary of dumping anything to the root of C: and would use %temp%\sysinfo instead.
0 like 0 dislike

The internal command ver reports windows version number (which could have been learned by typing help at the command prompt).

There is a dynamic variable %CMDEXTVERSION%, but it hasn't progressed in several releases so it's only useful for delineating between Windows NT and Windows 2000 and newer

Here's a batch to parse the output of ver for XP and newer, courtesy of Simon Sheppard:

@echo off
:: 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

::Winxp or 2003
if "%_minor%"=="2" goto sub_2003

Echo Windows 2003 or xp 64 bit [%PROCESSOR_ARCHITECTURE%]

if "%_minor%"=="1" goto sub7
Echo Windows Vista or Windows 2008 [%PROCESSOR_ARCHITECTURE%]

Echo Windows 7 or Windows 2008 R2 [%PROCESSOR_ARCHITECTURE%]

And here's my own fairly complete, largely academic, kick at the can which returns the parsed version number as environment variables:

@echo off
:: from
if %PROCESSOR_ARCHITECTURE%==x86   set pro_arch=32 Bit (x86)
if %PROCESSOR_ARCHITECTURE%==AMD64 set pro_arch=64 Bit (AMD64)
if %PROCESSOR_ARCHITECTURE%==IA64 set pro_arch=Itanium 64 Bit (IA64)

    call :clean
    for /f "tokens=2 delims=[]" %%x in ('ver') do set cmdver=%%x
    set cmdver=%cmdver:Version =%
    call :parse_cmdver
    call :ver%cmdver%
    call :Report
    goto :End

    :: Ensure we don't inherit values from previous runs
    set _verCmd=
    set _verMajor=
    set _verMinor=
    set _verBuild=
    set _verWin=
    goto :eof

    :: Turn "5.1.2306" string into actionable variables
    for /f "tokens=1,2,3* delims=." %%g in ("%cmdver%") do (
        set major=%%g
        set minor=%%h
        set build=%%i
    goto :eof

    echo.   CMD version is %cmdver%
    echo.   which probably means %longver% %pro_arch%
    goto :eof

    echo.   The numbers are stored in the following variables:
    set _ver
    goto :eof

::Table of version numbers built from 
    set longver=Windows 1.01
    set shortver=Win101
    goto :eof

    set longver=Windows 2.03
    set shortver=Win203
    goto :eof

    set longver=Windows 2.10
    set shortver=Win21
    goto :eof

    set longver=Windows 2.11
    set shortver=Win211
    goto :eof

    set longver=Windows 3.0
    set shortver=Win3
    goto :eof

    set longver=Windows 3.1, Windows For Workgroups 3.1, or Windows NT 3.1
    set shortver=Win31/WFW31/WinNT31
    goto :eof

    set longver=Windows For Workgroups 3.11
    set shortver=WFW311
    goto :eof

    set longver=Windows 3.2 (released in Simplified Chinese only)
    set shortver=Win32ch
    goto :eof

    set longver=Windows NT 3.5
    set shortver=WinNT35
    goto :eof

    set longver=Windows NT 3.51
    set shortver=WinNT351
    goto :eof

    set longver=Windows 95
    set shortver=Win95
    goto :eof

    set longver=Windows NT 4.0
    set shortver=WinNT4
    goto :eof

    set longver=Windows Me
    set shortver=WinMe
    goto :eof

    set longver=Windows 98
    set shortver=Win98
    goto :eof

    set longver=Windows 98 SE
    set shortver=Win98SE
    goto :eof

    set longver=Windows 2000
    set shortver=Win2K
    goto :eof

    set longver=Windows XP or Windows Fundamentals for Legacy PCs
    set shortver=WinXP/WinFun
    goto :eof

    set longver=Windows XP, Windows XP Pro or Windows Server 2003
    set shortver=WinXP/WinXP-Pro/Server2003
    goto :eof

    set longver=Windows Home Server
    set shortver=WinHomeServer
    goto :eof

    set longver=Windows Vista or Windows Server 2008
    set shortver=Vista/Server2008
    goto :eof

    set longver=Windows 7 or Windows Server 2008 R2
    set shortver=Win7/Server2008R2
    goto :eof

:: return version to calling shell/script,
:: see
endlocal & set _verCmd=%cmdver% & set _verMajor=%major% & set _verMinor=%minor% & set _verBuild=%build% & set _verWin=%shortver%
call :Report2 :: comment this line out to suppress extra reporting


answered May 29, 2013 by anonymous  
0 like 0 dislike
Type "ver" at a command prompt.

Next time around, since this isn't really programming related but server or user related
answered May 29, 2013 by anonymous