WMI tasks for computer hardware obtain information about the presence, state, or properties of hardware components. For example, you can determine whether a computer is a desktop or laptop. For other examples, see the TechNet ScriptCenter at http://www.microsoft.com/technet.
The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.
The following procedure describes how to run a script.
To run a script
- Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
- Open a command prompt window and navigate to the directory where you saved the file.
- Type cscript scriptfile.vbs at the command prompt.
Note By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can
produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at
the command prompt to redirect the output of the filename.vbs script to outfile.txt.
The following table lists script examples that can be used to obtain various types of data from the local computer.
| How do I... | WMI classes or methods |
| ...determine how much free memory a computer has? | Use the class Win32_OperatingSystem and the FreePhysicalMemory property.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
Wscript.Echo "Available Physical Memory: " & _
objOperatingSystem.FreePhysicalMemory
Next
|
| ...determine whether a computer has a DVD drive? | Use the Win32_CDROMDrive class and check for the acronym DVD in the Name or DeviceID property.
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
"Select * from Win32_CDROMDrive")
For Each objItem in colItems
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Name: " & objItem.Name
Next
|
| ...determine how much RAM is installed in a computer? | Use the Win32_ComputerSystem class and check the value of the TotalPhysicalMemory property.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
Wscript.Echo "System Name: " & objComputer.Name
Wscript.Echo "Total Physical Memory: " & _
objComputer.TotalPhysicalMemory
Next
|
| ...determine if a computer has more than one processor? | Use the Win32_ComputerSystem class and the property NumberOfProcessors.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
Wscript.Echo "System Name: " & objComputer.Name
Wscript.Echo "Number of Processors: " & _
objComputer.NumberOfProcessors
Next
|
| ...determine whether a computer has a PCMCIA slot? | Use the Win32_PCMCIAController class and check the value of the Count property. If Count is 0, then the computer has no PCMCIA slots.
strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_PCMCIAController")
Wscript.Echo "Number of PCMCIA slots: " _
& colItems.Count
|
| ...identify devices that are not working (those marked with an exclamation point icon in Device Manager)? | Use the Win32_PnPEntity class and use the following clause in your WQL query. WHERE ConfigManagerErrorCode <> 0
strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_PnPEntity " _
& "WHERE ConfigManagerErrorCode <> 0")
For Each objItem in colItems
Wscript.Echo "Class GUID: " & objItem.ClassGuid
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Wscript.Echo "Service: " & objItem.Service
Next
|
| ...determine the properties of the mouse used on computer? | Use the Win32_PointingDevice class. This returns the properties of all pointing devices, not just mouse devices.
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
"Select * from Win32_PointingDevice")
For Each objItem in colItems
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Device Interface: " _
& objItem.DeviceInterface
Wscript.Echo "Double Speed Threshold: " _
& objItem.DoubleSpeedThreshold
Wscript.Echo "Handedness: " & objItem.Handedness
Wscript.Echo "Hardware Type: " & objItem.HardwareType
Wscript.Echo "INF File Name: " & objItem.InfFileName
Wscript.Echo "INF Section: " & objItem.InfSection
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Number Of Buttons: " _
& objItem.NumberOfButtons
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Wscript.Echo "Pointing Type: " & objItem.PointingType
Wscript.Echo "Quad Speed Threshold: " _
& objItem.QuadSpeedThreshold
Wscript.Echo "Resolution: " & objItem.Resolution
Wscript.Echo "Sample Rate: " & objItem.SampleRate
Wscript.Echo "Synch: " & objItem.Synch
Next
|
| ...determine the speed of a processor installed in a computer? | Use the Win32_Processor class and check the value of the MaxClockSpeed property.
strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_Processor")
For Each objItem in colItems
Wscript.Echo "Processor Id: " & objItem.ProcessorId
Wscript.Echo "Maximum Clock Speed: " _
& objItem.MaxClockSpeed
Next
|
| ...determine whether a computer is a tower, a mini-tower, a laptop, and so on? | Use the Win32_SystemEnclosure class and check the value of the ChassisType property.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
For Each objItem in objChassis.ChassisTypes
Wscript.Echo "Chassis Type: " & objItem
Next
Next
|
| ...get the serial number and asset tag of a computer? | Use the Win32_SystemEnclosure class, and the properties SerialNumber and SMBIOSAssetTag.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSMBIOS = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
Wscript.Echo "Part Number: " & objSMBIOS.PartNumber
Wscript.Echo "Serial Number: " _
& objSMBIOS.SerialNumber
Wscript.Echo "Asset Tag: " _
& objSMBIOS.SMBIOSAssetTag
Next
|
| ...determine what kind of device is plugged into a USB port? | Use the Win32_USBHub class and check the Description property. This property may have a value such as "Mass Storage Device" or "Printing Support".
strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
"Select * from Win32_USBHub")
For Each objItem in colItems
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "PNP Device ID: " _
& objItem.PNPDeviceID
Wscript.Echo "Description: " _
& objItem.Description
Wscript.Echo
Next
|
| ...determine how many tape drives are installed on a computer? | Use the class Win32_TapeDrive class and then use the SWbemObjectSet.Count method. If Count = 0, then no tape drives are installed on the computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
"Select * from Win32_TapeDrive")
Wscript.Echo "Number of tape drives: " _
& colItems.Count
|
See Also
- WMI Tasks for Scripts and Applications
- WMI C++ Application Examples
- TechNet ScriptCenter
Send comments about this topic to Microsoft
Build date: 11/3/2009