I am trying to determine what operating system is
installed on a computer. I there a key in the
registry that holds this information.
thanks in advance
> I am trying to determine what operating system is
> installed on a computer. I there a key in the
> registry that holds this information.
Several ways:
----------------------------------------------------------------------------------------
1)
----------------------------------------------------------------------------------------
You can use the ver command from a script:
Function GetOS()
' using the ver command
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"
oShell.Run "%comspec% /c ver >" & sTempFile, 0, True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading,
FailIfNotExist, OpenAsASCII)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
Select Case True
Case InStr(sResults, "Windows 95") > 1 : GetOS = "W95"
Case InStr(sResults, "Windows 98") > 1 : GetOS = "W98"
Case InStr(sResults, "Windows Millennium") > 1 : GetOS = "WME"
Case InStr(sResults, "Windows NT") > 1 : GetOS = "NT4"
Case InStr(sResults, "Windows 2000") > 1 : GetOS = "W2k"
Case InStr(sResults, "Windows XP") > 1 : GetOS = "WXP"
Case Else : GetOS = "Unknown"
End Select
End Function
----------------------------------------------------------------------------------------
2)
----------------------------------------------------------------------------------------
If all your computers have WMI installed (WMI is preinstalled on
Win2000/WinXP/WinME and can be installed on NT 4.0/Win9x):
Set SystemSet = GetObject("winmgmts:"). _
InstancesOf("Win32_OperatingSystem")
for each System in SystemSet
WScript.Echo System.Caption
WScript.Echo "Version: " + System.Version
next
----------------------------------------------------------------------------------------
3)
----------------------------------------------------------------------------------------
Here is to functions for OS detection, GetOsVersion and GetOsVersionNumber. They
are based on registry values.
GetOsVersionNumber returns a nuber, so you can test using <, > and =. E.g. if
something works on all OS's except Win2k and WinXP, you can test like this:
If GetOsVersionNumber < 5 Then
MsgBox "OS is pre Win2k"
End If
Both scripts doesn't differ on W95A/W95B/W95C for W95 or W98/W98SE for W98, but
the functions is documented so this is easy to add if desired.
Function GetOsVersion()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Determines OS by reading reg val & comparing to known values
' OS type returned as:
' "W95", "W98", "WME", "WNT", "W2K", "WXP"
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim oShell, sOStype, sOSversion
Set oShell = CreateObject("WScript.Shell")
On Error Resume Next
sOStype = oShell.RegRead(_
"HKLM\SYSTEM\CurrentControlSet\Control\ProductOptions\ProductType")
If Err.Number<>0 Then
' Hex(Err.Number)="80070002"
' - Could not find this key, OS must be Win9x
Err.Clear
sOStype = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windows" & _
"\CurrentVersion\VersionNumber")
Select Case sOStype
Case "4.00.950"
sOStype = "W95" ' Windows 95A
Case "4.00.1111"
Dim sSubVersion
sSubVersion = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windows" & _
"\CurrentVersion\SubVersionNumber")
Select Case sSubVersion
Case " B"
sOStype = "W95" ' Windows 95B
Case " C"
sOStype = "W95" ' Windows 95C
Case Else
sOStype = "W95" ' Unknown Windows 95
End Select
Case "4.03.1214"
sOStype = "W95" ' Windows 95B
Case "4.10.1998"
sOStype = "W98" ' Windows 98
Case "4.10.2222"
sOStype = "W98" ' Windows 98SE
Case "4.90.3000"
sOStype = "WME" ' Windows Me
Case Else
sOstype = "W95" ' Unknown W9x/Me
End Select
Else ' OS is NT based
sOSversion = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
If Err.Number<>0 Then
GetOsVersion = "Unknown NTx"
' Could not determine NT version
Exit Function ' >>>
End If
Select Case sOSversion
Case "4.0"
sOStype = "WNT"
Case "5.0"
sOStype = "W2K"
Case "5.1"
sOStype = "WXP"
Case Else
sOStype = "Unknown OS"
End Select
End If
GetOsVersion = sOStype
End Function
Function GetOsVersionNumber()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Determines OS by reading reg val & comparing to known values
' OS version number returned as number of type double:
' Windows 95: 1
' Windows 98: 2
' Windows ME: 3
' Windows NT4: 4
' Windows 2k: 5
' Windows XP: 5.1
' Windows >XP: >5.1
' Note: Decimal point returned is based on the Locale setting
' of the computer, so it might be returned as 5,1 as well.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim oShell, sOStype, sOSversion
Set oShell = CreateObject("WScript.Shell")
On Error Resume Next
sOStype = oShell.RegRead(_
"HKLM\SYSTEM\CurrentControlSet\Control\ProductOptions\ProductType")
If Err.Number<>0 Then
' Hex(Err.Number)="80070002"
' - Could not find this key, OS must be Win9x
Err.Clear
sOStype = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windows" & _
"\CurrentVersion\VersionNumber")
Select Case sOStype
Case "4.00.950"
sOSversion = 1 ' Windows 95A
Case "4.00.1111"
Dim sSubVersion
sSubVersion = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windows" & _
"\CurrentVersion\SubVersionNumber")
Select Case sSubVersion
Case " B"
sOSversion = 1 ' Windows 95B
Case " C"
sOSversion = 1 ' Windows 95C
Case Else
sOSversion = 1 ' Unknown Windows 95
End Select
Case "4.03.1214"
sOSversion = 1 ' Windows 95B
Case "4.10.1998"
sOSversion = 2 ' Windows 98
Case "4.10.2222"
sOSversion = 2 ' Windows 98SE
Case "4.90.3000"
sOSversion = 3 ' Windows Me
Case Else
sOSversion = 1 ' Unknown W9x/Me
End Select
Else ' OS is NT based
sOSversion = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
If Err.Number<>0 Then
GetOsVersion = "Unknown NTx"
' Could not determine NT version
Exit Function ' >>>
End If
End If
' Setting Locale to "en-us" to be indifferent to country settings.
' CDbl might err else
SetLocale "en-us"
GetOsVersionNumber = CDbl(sOSversion)
End Function
--
torgeir