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. 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. |
| ...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. |
Related topics
Send comments about this topic to Microsoft
Build date: 3/9/2012
- 8/10/2011
- lldictator
# Get-NonworkingDevice.ps1
# Gets a list of non-working device
# Sample 4 on http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx
# Thomas Lee - tfl@psp.co.uk
# Get devices:
$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
""
}
This script produces the following output:
PSH [D:\foo]: .\get-nonworkingdevice.PS1'
Total Bad devices: 3
Name : Intel(R) PRO/Wireless 3945ABG Network Connection
Class Guid : {4D36E972-E325-11CE-BFC1-08002BE10318}
Description : Intel(R) PRO/Wireless 3945ABG Network Connection
Device ID : PCI\VEN_8086&DEV_4222&SUBSYS_10218086&REV_02\4&360A6DE&0&00E1
Manufacturer :
PNP Devcice Id : PCI\VEN_8086&DEV_4222&SUBSYS_10218086&REV_02\4&360A6DE&0&00E1
Service Name : w39n51
Name : 1394 Net Adapter
Class Guid : {4D36E972-E325-11CE-BFC1-08002BE10318}
Description : 1394 Net Adapter
Device ID : V1394\NIC1394\18B13C50444FC000
Manufacturer :
PNP Devcice Id : V1394\NIC1394\18B13C50444FC000
Service Name : NIC1394
Name : Microsoft Loopback Adapter
Class Guid : {4D36E972-E325-11CE-BFC1-08002BE10318}
Description : Microsoft Loopback Adapter
Device ID : ROOT\UNKNOWN\0000
Manufacturer :
PNP Devcice Id : ROOT\UNKNOWN\0000
Service Name : msloop
- 12/6/2008
- Thomas Lee
- 12/7/2008
- Thomas Lee
# Get-PCMCIA.ps1
# Sample 4 on http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx
# Gets the number of PCMCIA slots
# Thomas Lee - tfl@psp.co.uk
# Get system information
$Pcmcia = Get-WmiObject -Class Win32_PCMCIAController
if (!$pcmcia.count) {
"Number of PCMCIA Slots: {0}" -f 1
}else {
"Number of PCMCIA Slots: {0}" -f $pcmcia.count
}
This script produces the following output:
PSH [D:\foo]: .\get-pcmcia.PS1
Number of PCMCIA Slots: 1
- 12/6/2008
- Thomas Lee
- 12/7/2008
- Thomas Lee
# Get-Processor.ps1
# Sample 4 on http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx
# Gets the number of processors on the local system
# NB: processors includes all cores on mult-cores ystems
# Thomas Lee - tfl@psp.co.uk
# Get system information
$system = Get-WmiObject -Class Win32_ComputerSystem
# Display processor count
"System Name : {0}" -f $system.Name
"Number of Processors: {0}" -f $system.NumberOfProcessors
This script produces the followoing output (System is a single proc dual core system)
PSH [D:\foo]: .\get-processor.PS1'
System Name : UK0N055
Number of Processors: 2
- 12/6/2008
- Thomas Lee
- 12/7/2008
- Thomas Lee
# Get-PhysicalRAM.ps1
# Sample using PowerShell
# 3rd sample from http://msdn.microsoft.com/en-us/library/aa394587
# Get Physical RAM
# Thomas Lee
$mem = Get-WmiObject -Class Win32_ComputerSystem
# Display memory
"This system has {0} MB Free Memory" -f $($mem.TotalPhysicalMemory/1mb)
This script produces the following output:
PS C:\foo> .\get-physicalram
This system has 8188.7421875 MB Free Memory
- 11/29/2008
- Thomas Lee
- 12/7/2008
- Thomas Lee
# Get-DVD.ps1
# Get DVD Information sample using PowerShell
# 2nd sample from http://msdn.microsoft.com/en-us/library/aa394587
# Thomas Lee
$drives = Get-WmiObject -Class Win32_CDROMDrive
# Display CD info
$drives | Format-Table DeviceID, Description, Name -autosize
This script produces the following output:
PS C:\FOO> .\get-DVD.ps1
DeviceID Description Name
-------- ----------- ----
IDE\CDROMTSSTCORP_DVD+-RW_TS-H653B_______________D200____\5&1E5D3123&0&0.0.0 CD-ROM Drive TSSTcorp DVD+-RW TS-H653B ATA Device
IDE\CDROMTSSTCORP_DVD-ROM_TS-H353B_______________D200____\5&3116E1C8&0&1.0.0 CD-ROM Drive TSSTcorp DVD-ROM TS-H353B ATA Device
SCSI\CDROM&VEN_GF7300L&PROD_AZZ836K&REV_1.01\5&36E5972&1&000000 CD-ROM Drive GF7300L AZZ836K SCSI CdRom Device
- 11/29/2008
- Thomas Lee
- 12/7/2008
- Thomas Lee
# Get-FreeMemory.ps1
# Sample using PowerShell
# 1st sample from http://msdn.microsoft.com/en-us/library/aa394587
# Thomas Lee
$mem = Get-WmiObject -Class Win32_OperatingSystem
# Display memory
"System : {0}" -f $mem.csname
"Free Memory: {0}" -f $mem.FreePhysicalMemory
This script produces the following output:
PS C:\foo> .\get-freememory.ps1
System : COOKHAM8
Free Memory: 2776988
- 11/29/2008
- Thomas Lee
- 12/7/2008
- Thomas Lee
# Get-Mouseinfo.ps1
# Sample 7 on http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx
# Gets properties of the mouse using Win32_PointingDevice
# Thomas Lee - tfl@psp.co.uk
# Get Mouse information
$mouse = Get-WmiObject -Class Win32_PointingDevice
# Decode detalis
function Deviceinterface {
param ($value)
switch ($value) {
0 {"Other"}
1 {"Unknown"}
3 {"Serial"}
4 {"PS/2"}
5 {"Infrared"}
6 {"HP-HIL"}
7 {"Bus Mouse"}
8 {"ADP (Apple Desktop Bus)"}
160 {"Bus Mouse DB-9"}
161 {"Bus Mouse Micro-DIN"}
162 {"USB"}
}
}
function Handedness {
param ($value)
switch ($value) {
0 {"Unknown"}
1 {"Not Applicable"}
2 {"Right-Handed Operation"}
3 {"Left-Handed Operation"}
}
}
function Pointingtype {
param ($value)
switch ($value) {
1 {"Other"}
2 {"Unknown"}
3 {"Mouse"}
4 {"Track Ball"}
5 {"Track Point"}
6 {"Glide Point"}
7 {"Touch Pad"}
8 {"Touch Screen"}
9 {"Mouse - Optical Sensor"}
}
}
# Display details
"Mouse Information on System: {0}" -f $mouse.systemname
"Description : {0}" -f $mouse.Description
"Device ID : {0}" -f $mouse.DeviceID
"Device Interface : {0}" -f (Deviceinterface($mouse.DeviceInterface))
"Double Speed Threshold : {0}" -f $mouse.DoubleSpeedThreshold
"Handedness : {0}" -f (Handedness($mouse.handedness))
"Hardware Type : {0}" -f $mouse.Hardwaretype
"INF FIle Name : {0}" -f $mouse.InfFileName
"Inf Section : {0}" -f $mouse.InfSection
"Manufacturer : {0}" -f $mouse.Manufacturer
"Name : {0}" -f $mouse.Name
"Number of buttons : {0}" -f $mouse.NumberOfButtons
"PNP Device ID : {0}" -f $mouse.PNPDeviceID
"Pointing Type : {0}" -f (Pointingtype ($mouse.PointingType))
"Quad Speed Threshold : {0}" -f $mouse.QuadSpeedThreshold
"Resolution : {0}" -f $mouse.Resolution
"Sample Rate : {0}" -f $mouse.SampleRate
"Synch : {0}" -f $mouse.Synch
This script produces the following output:
PSH [D:\foo]: .\get-mouseinfo.ps1
Mouse Information on System: UK0N055
Description : Alps Touch Pad
Device ID : ACPI\PNP0F13\4&25E2FF18&0
Device Interface : Unknown
Double Speed Threshold : 6
Handedness : Right-Handed Operation
Hardware Type : Alps Touch Pad
INF FIle Name : oem0.inf
Inf Section : MouFilter_Inst
Manufacturer : Alps Electric
Name : Alps Touch Pad
Number of buttons : 2
PNP Device ID : ACPI\PNP0F13\4&25E2FF18&0
Pointing Type : Unknown
Quad Speed Threshold : 10
Resolution :
Sample Rate :
Synch :
- 12/7/2008
- Thomas Lee