Using scripts and WMI to ask a computer more about itself

When you sit down to a PC hoping to troubleshoot a problem, the more information you can have about it the better. Sometimes that means crawling under a desk, opening the case, or visiting various locations within Windows to find all the properties you are wanting. If you want to get fancy, you could write a script to get all your information from a single place. Of course, this is just a single use case and there are plenty of other ways this information could be used.

The key to getting this information from the computer is WMI, Windows Management Instrumentation, extensions to the Windows Driver Model that allows us to interface with the operating system and hardware.

There are various ways to utilize WMI. It offers its own command line tool on Windows XP Pro, Server 2003, Vista, Win7, and Server 2008 with WMIC. In the command prompt run WMIC /? for more information.

As an example, here I am querying the architecture (32-bit or 64-bit) of the operating system:

WMIC OS GET OSArchitecture

It returns a simple answer of 64-bit.

Scripting with PowerShell and VBScript are other ways to access the WMI and are more common. They offer a lot more flexibility of what to do with the results and with a better interface. To find the information you want, you will have to query the correct class. Each class has a variety of properties within them that might contain the values you seek. This table shows some of the common ones I used in the example scripts at the bottom.

Classes Description
Win32_Product Finding out about software installed by Windows Installer, particularly Name and Version
Win32_BIOS Query information from the BIOS like the PC’s serial number and manufacturer
Win32_Processor Find out about the CPU (Number of cores, name, clockspeed)
Win32_ComputerSystem Find out about the PC (Manufacturer, Model, and much more)
Win32_OperatingSystem Find out about the OS (active operating system’s name, version, etc)

The Windows Dev Center has a great collection of example scripts split into various areas of usefulness in theĀ WMI Tasks for Scripts and Applications. My scripts extend their examples for properties I found important and useful. The MSDN site has many more example scripts that work out of the box and are ready for tweaking. Just grab your favorite text editor (I use Notepad++) and save the file with a .vbs file extension.

One of the useful, impressive scripts follows. It queries the software installed by Windows Installer and returns the name and version of the software.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
("Select * from Win32_Product")For Each objSoftware in colSoftware
Wscript.Echo "Name: " & objSoftware.Name, vbCRLF & "Version: " & objSoftware.Version

My thought is that I could take this script, narrow it down to the key applications, and convert the script to write out to a file instead of a popup. I would have this script run at login and save the file to a centralized server share. This would allow me to aggregate the data and come up with a consensus of what percentage of computers are up to date and which computers need to be updated.

(To copy multiple .txt files into one, use the command ‘copy *.txt final.txt’ in a directory of the text files.)

I adjusted the query here to only get Adobe Reader and Adobe Flash Player:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
("Select * from Win32_Product Where (Name like 'Adobe Reader%') OR (Name like 'Adobe Flash Player%')")For Each objSoftware in colSoftware
Wscript.Echo "Name: " & objSoftware.Name, vbCRLF & "Version: " & objSoftware.Version

A longer script, this uses a variety of classes to bring up various information about the computer. You can just comment out the areas for information not needed by adding a ‘ to the left of the line.

'Interrogate computer for a wide variety of self-identifying properties using Win32_ComputerSystem, Win32_BIOS, and other classes
'Script written by Jason Hamilton,

Dim objWMI
Set objWMI = GetObject("winmgmts:")
Dim colSettingsComp
Set colSettingsComp = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
Dim colSettingsBios
Set colSettingsBios = objWMI.ExecQuery("Select * from Win32_BIOS")
Dim colSettingsRAM
Set colSettingsRAM = objWMI.ExecQuery("Select * from Win32_PhysicalMemory")
Dim colSettingsCPU
Set colSettingsCPU = objWMI.ExecQuery("Select * from Win32_Processor")
Dim colSettingsOS
Set colSettingsOS = objWMI.ExecQuery("Select * from Win32_OperatingSystem")

Dim objComputer

' Reports PC Manufacturer
Dim strManu
For Each objComputer in colSettingsBios
 strManu = objComputer.Manufacturer
wscript.echo "Manufacturer: " & strManu

'Reports computers identified Model name
Dim strModel
For Each objComputer in colSettingsComp
 strModel = objComputer.Model
wscript.echo "Model: " & strModel

' Reports serial number
Dim strSerial
For Each objComputer in colSettingsBios
 strSerial = objComputer.SerialNumber
wscript.echo "Serial number: " & strSerial

' Reports Physical Memory installed
' Capacity from Win32_PhysicalMemory is more accurate but only returns the value per stick
Dim strRAMB, strRAMKB, strRAMMB, strRAMGB
For Each objComputer in colSettingsComp
 strRAM = objComputer.TotalPhysicalMemory
' Dividing to convert from bytes to gigabytes
strRAMKB = strRAM / 1024
strRAMMB = strRAMkb / 1024
strRAMGB = strRAMMB / 1024
wscript.echo "Amount of RAM: " & Round(strRAMGB,2) & "GB"

'Reports info about CPU
Dim strCPU, numCPU, numCores, CPUManu, CPUName

'Number of processors
For Each objComputer in colSettingsComp
 numCPU = objComputer.NumberOfProcessors

'Processor manufacturer
For Each objComputer in colSettingsCPU
 CPUManu = objComputer.Manufacturer

'Processor name
For Each objComputer in colSettingsCPU
 CPUName = objComputer.Name

'Number of cores
For Each objComputer in colSettingsCPU
 numCores = objComputer.NumberOfCores

'CPU clockspeed
For Each objComputer in colSettingsCPU
 strCPU = objComputer.MaxClockSpeed
strCPU = strCPU / 1000

'CPU Report output
wscript.echo numCPU & " " & CPUManu & " " & CPUName, vbCRLF & numCores & " cores @" & strCPU & "GHz"

'Operating system information
Dim strOSName, strOSArch
For Each objComputer in colSettingsOS
 strOSName = objComputer.Caption
For Each objComputer in colSettingsComp
 strOSArch = objComputer.SystemType
wscript.echo "OS: " & strOSName & " " & strOSArch

WordPress is converting the ampersands and other symbols into various web characters, so the scripts above probably won’t work if you copy/paste them into your text editor. You can download the scripts from the 404TechSupport server. They have been tested on Windows 7 x64.

Comments are closed.