WMI Tasks: Computer Hardware
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, such as filename.vbs. 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 filename.vbs at the command prompt.
- If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).
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 Note that this code may not detect USB devices that are missing drivers. 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 |
Examples
The following TechNet Gallery example code describes how to list the free space of all drives for several machines.
The Get-ComputerInfo - Query Computer Info From Local/Remote Computers - (WMI) PowerShell sample on TechNet Gallery uses a number of calls to hardware and software to display information about a local or remote system.
The Multithreaded System Asset Gathering with Powershell PowerShell sample on TechNetGallery gathers a plethora of useful system information via WMI and multithreading with powershell.
Related topics