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 https://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.
To run a script
The following procedure describes how 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).
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 filename.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.
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
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
PowerShell
"System Name : {0}" -f $system.Name
"Number of Processors: {0}" -f $system.NumberOfProcessors
...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.
VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PCMCIAController")
Wscript.Echo "Number of PCMCIA slots: " & colItems.Count
if (!$pcmcia.count) {
"Number of PCMCIA Slots: {0}" -f 1
}else {
"Number of PCMCIA Slots: {0}" -f $pcmcia.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.
VB
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
PowerShell
$baddevices = Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}
" Total Bad devices: {0}" -f $baddevices.count
foreach ($device in $baddevices) {
"Name : {0}" -f $device.name
"Class Guid : {0}" -f $device.Classguid
"Description : {0}" -f $device.Description
"Device ID : {0}" -f $device.deviceid
"Manufacturer : {0}" -f $device.manufactuer
"PNP Devcice Id : {0}" -f $device.PNPDeviceID
"Service Name : {0}" -f $device.service
""
}
...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.
...determine the speed of a processor installed in a computer?
Use the Win32_Processor class and check the value of the MaxClockSpeed property.
VB
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.
VB
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
...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.
VB
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
PowerShell
$colItems = Get-WmiObject -Class Win32_TapeDrive
foreach ($objItem in $colItems)
{
"Number of Drives: " + $colItems.Count
}
Examples
The "Multithreaded System Asset Gathering with PowerShell" PowerShell sample gathers a plethora of useful system information via WMI and multithreading with powershell.
This learning path covers Windows Management Instrumentation (WMI) and Common Information Model (CIM). These technologies help to access information about a computer. Additionally, both technologies provide local and remote access to management information from the operating system, computer hardware, and installed software.