Get OS Version

Forum for anything else which doesn't fit in the above forums. Site feedback, random talk, whatever, are welcome.
Post Reply
User avatar
vmanda
Posts: 1634
Joined: Wed Apr 18, 2007 12:25 pm
Location: TM.Romania

Get OS Version

Post by vmanda » Fri Jul 20, 2012 4:11 pm

Command script writers, i propose you another challenge.

I have writted this vbs script to get windows version from registry:
get_version.vbs wrote:Option Explicit
Dim WSHShell, RegKey
Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\Software\Microsoft\Windows NT\CurrentVersion" & "CurrentVersion"
WScript.Echo WSHShell.RegRead (RegKey)
Runned at command prompt this way:
Image
at command prompt wrote:cscript.exe //nologo get_version.vbs
this will return, for exemple 5.1 for XP.
returned [b]CurrentVersion[/b] values wrote:Win2K - CurrentVersion = 5.0
WinXP - CurrentVersion = 5.1
Win2K3, WinXPx64 - CurrentVersion = 5.2
Vista - CurrentVersion = 6.0
Seven - CurrentVersion = 6.1
How to create a command script to execute different commands depending of OS?
Last edited by vmanda on Fri Jul 20, 2012 4:28 pm, edited 5 times in total.

User avatar
5eraph
Site Admin
Posts: 4618
Joined: Tue Jul 05, 2005 9:38 pm
Location: Riverview, MI USA

Post by 5eraph » Fri Jul 20, 2012 4:18 pm

WinXPx64 is also 5.2. ;)

User avatar
vmanda
Posts: 1634
Joined: Wed Apr 18, 2007 12:25 pm
Location: TM.Romania

Post by vmanda » Fri Jul 20, 2012 4:24 pm

5eraph wrote:WinXPx64 is also 5.2. ;)
Kernel for WinXPx64 is from Win2k3 ?

The bitness can be checked separately.

User avatar
5eraph
Site Admin
Posts: 4618
Joined: Tue Jul 05, 2005 9:38 pm
Location: Riverview, MI USA

Post by 5eraph » Fri Jul 20, 2012 4:32 pm

Yes, XPx64 was built from 2K3x64 SP1.

Depending on how you check it, Win2K3x64 will look like WinXPx64. Microsoft checks the following value in its hotfix update packages:

Code: Select all

HKLM,"SYSTEM\CurrentControlSet\Control\ProductOptions","ProductType",0
"ServerNT" = Win2k3x64
"WinNT" = WinXPx64

tomasz86
Posts: 491
Joined: Tue Aug 09, 2011 12:12 pm
Location: windowsarchives.com
Contact:

Post by tomasz86 » Fri Jul 20, 2012 5:23 pm

It would be also nice to be able to distinguish between each edition, ex. XP Home/Pro or Server/Advanced Server/Datacenter Server, etc.

User avatar
vmanda
Posts: 1634
Joined: Wed Apr 18, 2007 12:25 pm
Location: TM.Romania

Post by vmanda » Fri Jul 20, 2012 6:16 pm

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion]
"ProductName"="Microsoft Windows XP"
"CurrentVersion"="5.1"
"CurrentBuildNumber"="2600"
"CSDVersion"="Service Pack 3"
Same vbs script can be used to query any of this reg keys under same reg brache.
For example:
Option Explicit
Dim WSHShell, Reg_1, Reg_2, Reg_3, Reg_4
Set WSHShell = CreateObject("WScript.Shell")
Reg_1 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "ProductName"
Reg_2 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "CSDVersion"
Reg_3 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "CurrentVersion"
Reg_4 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "CurrentBuildNumber"
WScript.Echo WSHShell.RegRead (Reg_1) & Chr(13) & Chr(10) & WSHShell.RegRead (Reg_2) & Chr(13) & Chr(10) & "Build: " & WSHShell.RegRead (Reg_3) & "." & WSHShell.RegRead (Reg_4)
produce:
Image or Image

so you can query any reg values without the verbose that command line reg add.
Also that is the reason that make me write this vbs script, to output reg values without any other verbose.

User avatar
user_hidden
Posts: 1924
Joined: Thu Dec 06, 2007 7:52 am
Location: Canada eh!

Post by user_hidden » Fri Jul 20, 2012 7:48 pm

Code: Select all

@echo off
Title "Display Operating System & Build Information"
for /f "tokens=2 delims==" %%A in ('"WMIC OS LIST BRIEF /format:list"') do set build=%%A
if %build% equ 5.1.2600 set name=Windows XP SP3
if %build% equ 6.0.6002 set name=Windows Vista SP2
if %build% equ 6.1.7600 set name=Windows 7
if %build% equ 6.1.7601 set name=Windows 7 SP1
echo.
echo. Operating System: %name%
echo. Build Version   : %build%
echo.
pause

User avatar
vmanda
Posts: 1634
Joined: Wed Apr 18, 2007 12:25 pm
Location: TM.Romania

Post by vmanda » Fri Jul 20, 2012 8:08 pm

ECHO Option Explicit > version.vbs
ECHO Dim WSHShell, RegKey >> version.vbs
ECHO Set WSHShell = CreateObject("WScript.Shell") >> version.vbs
ECHO RegKey = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion" >> version.vbs
ECHO WScript.Echo WSHShell.RegRead (RegKey) >> version.vbs
FOR /F "tokens=*" %%A IN ('cscript.exe //nologo version.vbs') DO SET CurrentVersion=%%A
Using something like that we can query any registry values and use it
into our command scripts.

In this example the orange is the root of regkey and yellow is regkey itself.
To not carry the vbs file i just echoed few lines to create it.
same results like user_hidden wmic use wrote:ECHO Option Explicit > version.vbs
ECHO Dim WSHShell, Version , BuildNumber >> version.vbs
ECHO Set WSHShell = CreateObject("WScript.Shell") >> version.vbs
ECHO Version = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion" >> version.vbs
ECHO BuildNumber = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber" >> version.vbs
ECHO WScript.Echo WSHShell.RegRead (Version) ^& "." ^& WSHShell.RegRead (BuildNumber) >> version.vbs

FOR /F "tokens=*" %%A IN ('cscript.exe //nologo version.vbs') DO SET BUILD=%%A

IF %BUILD% EQU 5.1.2600 SET NAME=Windows XP SP3
IF %BUILD% EQU 6.0.6002 SET NAME=Windows Vista SP2
IF %BUILD% EQU 6.1.7600 SET NAME=Windows 7
IF %BUILD% EQU 6.1.7601 SET NAME=Windows 7 SP1

ECHO %NAME% %BUILD%
DEL /F /Q version.vbs
Note: when ECHO special characters like &, this needs to be escaped ^&

User avatar
vmanda
Posts: 1634
Joined: Wed Apr 18, 2007 12:25 pm
Location: TM.Romania

Post by vmanda » Fri Jul 20, 2012 9:17 pm

The following table lists some of the common build numbers:

Version - Windows Name and/or Service Pack
------- --------------------------------------------------
5.0.2195 Windows 2000
5.1.2600 Windows XP
5.1.2600 Windows XP 64-Bit Edition Version 2002 (Itanium)
5.2.3790 Windows Server 2003
5.2.3790 Windows XP x64 Edition (AMD64/EM64T)
5.2.3790 Windows XP 64-Bit Edition Version 2003 (Itanium)
6.0.6000 Windows Vista
6.0.6001 Windows Vista with Service Pack 1
6.0.6002 Windows Vista with Service Pack 2
6.0.6001 Windows Server 2008
6.1.7600 Windows 7
6.1.7600 Windows Server 2008 R2
6.1.7601 Windows 7 with Service Pack 1
6.1.7601 Windows Server 2008 R2 with Service Pack 1
6.2.8400 Windows 8 Release Preview

Comparison_of_Microsoft_Windows_versions

User avatar
mr_smartepants
Posts: 824
Joined: Thu May 18, 2006 5:56 am
Location: Cambridgeshire, UK

Post by mr_smartepants » Sat Jul 21, 2012 1:37 am

The answer you seek can be found by polling the registry also instead of using WMIC (which can be slow).
Here's the batch script code I use in my SAD2 DriverPacks script. It can be altered to look for "CurrentVersion" instead.

Code: Select all

:OS-check
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentBuildNumber') DO SET build=%%B
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName') DO SET prodname=%%B
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v InstallationType') DO SET installtype=%%B
SET "OSTYPE=WINXP"
FOR %%i IN (2600 3790) DO (
IF /I "%build%"=="%%i" SET "OSbuild=NT5"
  )
FOR %%i IN (6000 6001 6002 7600 7601 7602) DO (
IF /I "%build%"=="%%i" SET "OSbuild=NT6"
  )
FOR %%i IN (6000 6001 6002) DO (
IF /I "%build%"=="%%i" SET "OSTYPE=VISTA"
  )
FOR %%i IN (7600 7601 7602) DO (
IF /I "%build%"=="%%i" SET "OSTYPE=WIN7"
  )
IF /I "%INSTALLTYPE%"=="Server" SET "OSTYPE=SERVER"
Altering the above code like so:

FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion') DO SET B_version=%%B

Then you can play around with the array processing to build your table.
Image
Some heroes don't wear capes, they wear Kevlar and dog-tags!

tomasz86
Posts: 491
Joined: Tue Aug 09, 2011 12:12 pm
Location: windowsarchives.com
Contact:

Post by tomasz86 » Sat Jul 21, 2012 2:01 am

How about using "ver" in cmd.exe?

Code: Select all

ver
This is the output here:

Code: Select all

Microsoft Windows 2000 [Version 5.00.2195]

User avatar
bphlpt
Posts: 1361
Joined: Sat Apr 19, 2008 1:11 am

Post by bphlpt » Sat Jul 21, 2012 3:13 am

Another option is to use the built-in Windows function "GetVersionEx" - http://msdn.microsoft.com/en-us/library ... s.85).aspx. To make it easier to get to the information, a GUI version is available - http://sourceforge.net/projects/getversionex/ - which comes as a command-line utility back-end, both 32bit and 64bit versions, and a windows GUI front-end. Example GUI output:

Image

[For wProductType - 1=Workstation, 2=Domain Controller, 3=Server]

As to "ver" in cmd.exe, for Win7 x64, the output is:

Code: Select all

Microsoft Windows [Version 6.1.7601]
Cheers and Regards

User avatar
yumeyao
Moderator
Posts: 1718
Joined: Sun Aug 27, 2006 9:24 pm
Location: Taiyuan, Shanxi, PR China

Post by yumeyao » Sat Jul 21, 2012 5:36 am

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

doesn't this work?

Just add a wrapper to this command, for example, 'for', like this:


for /f "tokens=1,2,3" %a in ('reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion') do if "%a"=="CurrentVersion" set osver=%c
Image
My work list(Hosted by dumpydooby)

Post Reply