You are not logged in.

#1 25 Nov 2009 01:11

maphew
Member
Registered: 04 Apr 2007
Posts: 7
Website

determine windows (cmd.exe) version from inside a batchfile?

How to (most) reliably determine what version of Windows or CMD.exe one is running under? The example on the reference page (http://ss64.com/nt/ver.html) doesn't work on 64bit XP or Windows7. On those machines, or at least the ones I have, the delineating name is missing. The output of xp32, xp64, and w7x64 respectively:

Microsoft Windows XP [Version 5.1.2600]
Microsoft Windows [Version 5.2.3790]
Microsoft Windows [Version 6.1.7600]

Is there a comprehensive list somewhere of what windows releases the version numbers correspond too?

Offline

#2 25 Nov 2009 01:24

maphew
Member
Registered: 04 Apr 2007
Posts: 7
Website

Re: determine windows (cmd.exe) version from inside a batchfile?

perhaps the "Current Version / Build" column from http://en.wikipedia.org/wiki/Microsoft_ … f_releases  ?

Offline

#3 25 Nov 2009 05:14

insthink
Member
Registered: 24 Oct 2009
Posts: 51

Re: determine windows (cmd.exe) version from inside a batchfile?

Im not sure what you're trying to find.

Are you trying to get the Windows version or the CMD version? because as you've just shown, "ver" works fine to give you the cmd version.

If you're looking for the OS, then type: echo %os%

Offline

#4 25 Nov 2009 09:05

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: determine windows (cmd.exe) version from inside a batchfile?

Not sure if that version number table is going to help you or not, as it appears several different editions have the same value -- look at XP 64bit, Win2003.

There was a recent thread along these lines, with lots of replies.  Have a good read through this and see if it helps http://ss64.org/oldforum/viewtopic.php?id=803

Last edited by bluesxman (25 Nov 2009 09:07)


cmd | *sh | ruby | chef

Offline

#5 25 Nov 2009 11:42

Chimaera
Member
Registered: 24 Aug 2009
Posts: 134

Re: determine windows (cmd.exe) version from inside a batchfile?

http://ss64.org/oldforum/viewtopic.php?id=803

Its still unsolved at this time with the help of many here we took it so far but not completed

still hoping it will be solved eventually

The WMIC section works well but the VER seection not so well

Last edited by Chimaera (25 Nov 2009 11:42)

Offline

#6 25 Nov 2009 12:05

insthink
Member
Registered: 24 Oct 2009
Posts: 51

Re: determine windows (cmd.exe) version from inside a batchfile?

Maybe there's a location in the registry with such information, then drop "ver" all together.

Offline

#7 25 Nov 2009 19:24

maphew
Member
Registered: 04 Apr 2007
Posts: 7
Website

Re: determine windows (cmd.exe) version from inside a batchfile?

insthink wrote:

Im not sure what you're trying to find.

Are you trying to get the Windows version or the CMD version? because as you've just shown, "ver" works fine to give you the cmd version.

If you're looking for the OS, then type: echo %os%

OS tells me Windows_NT for both Windows7 and XP, and if memory serves hasn't changed since Windows NT 3.5.

ver gives me a version number for cmd, but that on it's own is not enough as I don't know if "5.2.3790" is XP or Server 2003 (or msdos 5 for that matter). Perhaps this doesn't actually matter though, for my immediate purpose anyway.

Maybe there's a location in the registry with such information, then drop "ver" all together.

I've learned everything to answer this question lies under "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" and reg.exe  can retrieve it. Not sure the reg.exe is always available though(?)

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion

thanks. I'd never heard of wmic before.

Offline

#8 25 Nov 2009 19:28

DoorToDoorGeek
Member
Registered: 24 Nov 2009
Posts: 2

Re: determine windows (cmd.exe) version from inside a batchfile?

Just a note

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion

Does not work on Win2k

Offline

#9 25 Nov 2009 21:23

Chimaera
Member
Registered: 24 Aug 2009
Posts: 134

Re: determine windows (cmd.exe) version from inside a batchfile?

on Windows 7 64 bit

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion

gives me

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    CurrentVersion    REG_SZ    6.1

and based on the release schedule

    Windows 7 and Windows Server 2008 R2     NT 6.1.7600

so 6.1 sounds about right

and ver gives me

Microsoft Windows [Version 6.1.7600]

so maybe my origanal question about was based on incorrect assumptions

and we should be aiming for the numbers instead

Last edited by Chimaera (25 Nov 2009 21:32)

Offline

#10 25 Nov 2009 22:00

Chimaera
Member
Registered: 24 Aug 2009
Posts: 134

Re: determine windows (cmd.exe) version from inside a batchfile?

Try this it works on win 7 64bit and xp 32bit for me

@echo off
::==================================
::variables
if %PROCESSOR_ARCHITECTURE%==x86   set pro_arch=32 Bit
if %PROCESSOR_ARCHITECTURE%==AMD64 set pro_arch=64 Bit
if %PROCESSOR_ARCHITECTURE%==IA64 set pro_arch=Itanium 64 Bit
::==================================
ver | find "4.0.950">nul
if not errorlevel 1 (
set WinOS=Windows 95
goto OSis)
::==================================
ver | find "4.10.1998">nul
if not errorlevel 1 (
set WinOS=Windows 98
goto OSis)
::==================================
ver | find "4.10.2222">nul
if not errorlevel 1 (
set WinOS=Windows 98 SE
goto OSis)
::==================================
ver | find "4.90.3000">nul
if not errorlevel 1 (
set WinOS=Windows ME
goto OSis)
::==================================
ver | find "NT">nul
if not errorlevel 1 (
set WinOS=Windows NT
goto OSis)
::==================================
ver | find "5.0.2195">nul
if not errorlevel 1 (
set WinOS=Windows 2000
goto OSis)
::==================================
ver | find "5.1.2600">nul
if not errorlevel 1 (
set WinOS=Windows XP
goto OSis)
::==================================
ver | find "5.2.3790">nul
if not errorlevel 1 (
set WinOS=Windows 2003
goto OSis)
::==================================
ver | find "6.0.6002">nul
if not errorlevel 1 (
set WinOS=Windows Vista
goto OSis)
::==================================
ver | find "5.2.4500">nul
if not errorlevel 1 (
set WinOS=Windows Home Server
goto OSis)
::==================================
ver | find "6.1.7600">nul
if not errorlevel 1 (
set WinOS=Windows 7)

::==================================
:OSis
echo.
echo.
echo.Your Operating System Is %WinOS% %pro_arch%
echo.
echo.
@pause

If im correct this should work and ive added the 32/64 bit identification into it

Can u  please note which OS it does work on so we can check plz

Last edited by Chimaera (25 Nov 2009 22:17)

Offline

#11 26 Nov 2009 23:16

maphew
Member
Registered: 04 Apr 2007
Posts: 7
Website

Re: determine windows (cmd.exe) version from inside a batchfile?

W7x64 reports "Windows 7 64 Bit"
XPx64  reports "Windows 2003 64 Bit"
XPx32 reports "Windows XP 32 Bit"

Offline

#12 27 Nov 2009 00:00

maphew
Member
Registered: 04 Apr 2007
Posts: 7
Website

Re: determine windows (cmd.exe) version from inside a batchfile?

Here's my kick at the can, building on Chimeara's:
(((UPDATES:
r2 - updated to clean up duplicate x64 reporting
r3 - store parsed version number in _ver*
r4 - merge redundant routines
)))

@echo off
setlocal
:: from http://ss64.org/viewtopic.php?pid=3136#p3136
::==================================
::variables
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)

:Main
    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


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

:Parse_cmdver
    :: 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

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

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


::Table of version numbers built from 
:: http://en.wikipedia.org/wiki/Microsoft_Windows#Timeline_of_releases
:ver1.01
    set longver=Windows 1.01
    set shortver=Win101
    goto :eof

:ver2.03
    set longver=Windows 2.03
    set shortver=Win203
    goto :eof

:ver2.10
    set longver=Windows 2.10
    set shortver=Win21
    goto :eof

:ver2.11
    set longver=Windows 2.11
    set shortver=Win211
    goto :eof

:ver3.0
    set longver=Windows 3.0
    set shortver=Win3
    goto :eof

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

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

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

:ver3.5
    set longver=Windows NT 3.5
    set shortver=WinNT35
    goto :eof

:ver3.51
    set longver=Windows NT 3.51
    set shortver=WinNT351
    goto :eof

:ver4.0.950
    set longver=Windows 95
    set shortver=Win95
    goto :eof

:ver4.0.1381
    set longver=Windows NT 4.0
    set shortver=WinNT4
    goto :eof

:ver4.90.3000
    set longver=Windows Me
    set shortver=WinMe
    goto :eof

:ver4.10.1998
    set longver=Windows 98
    set shortver=Win98
    goto :eof

:ver4.10.2222
    set longver=Windows 98 SE
    set shortver=Win98SE
    goto :eof

:ver5.0.2195
    set longver=Windows 2000
    set shortver=Win2K
    goto :eof

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

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

:ver5.2.4500
    set longver=Windows Home Server
    set shortver=WinHomeServer
    goto :eof

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

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


:End
:: return version to calling shell/script,
:: see http://ss64.com/nt/syntax-functions.html
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

Last edited by maphew (27 Nov 2009 18:21)

Offline

#13 27 Nov 2009 12:36

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: determine windows (cmd.exe) version from inside a batchfile?

maphew wrote:

Here's my kick at the can, building on Chimeara's:

Thats good but you have several duplicate subroutine names:
:ver3.1
:ver3.1
:ver5.1.2600
:ver5.2.3790
:ver5.2.3790
:ver5.2.3790
:ver5.1.2600
:ver6.0.6002

I think in practice theres little to be gained from distinguishing between windows versions with the same build number. Its more important to have the 32/64 architecture and (in recent OS's) the features that are installed.

What can be really useful is a way to distinguish all workstations from servers e.g. you may not want to run a login script when logging into a server. We do this by having a naming convention that prefixes every server name with one short string and every workstation name with a different short string - this gives you the option to use a server OS on some workstations - as long as it's named correctly (as a workstation) everything still works.

If we look at only the major and minor version numbers and ignore the build release no. then theres no worry about the script breaking as soon as the next service pack is released.

@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

Offline

#14 27 Nov 2009 18:33

maphew
Member
Registered: 04 Apr 2007
Posts: 7
Website

Re: determine windows (cmd.exe) version from inside a batchfile?

thanks for pointing out the redundancies. I've cleaned and reposted for the sake of completeness, but you're right it's largely an academic exercise to go through the trouble of distinguishing builds. Thanks for the simpler example.

Here's the question and thread which led me here, thanks for all the help!
http://stackoverflow.com/questions/1792 … running-on

Last edited by maphew (27 Nov 2009 18:43)

Offline

#15 28 Nov 2009 12:48

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: determine windows (cmd.exe) version from inside a batchfile?

I think you've over cooked the goose with regard to the amount of legacy Windows versions you're checking for -- not least because some of the commands you're using using simply won't work on anything that's not NT based (e.g. Win95, Win98) and/or older than WinNT4.


cmd | *sh | ruby | chef

Offline

Board footer

Powered by