How Can I Get a List of the Devices That Aren’t Working?

Doctor Scripto

Hey, Scripting Guy! Question

Hey, Scripting Guy! In Device Manager, any time a device isn’t working that device is marked with a yellow exclamation mark. How can I use a script to get a list of the devices that aren’t working?

— OL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, OL. Sometimes in baseball a batter absolutely hammers one, but the ball takes a lucky bounce and lands right in your glove. That’s often referred to as a “candy hop.” Turns out that this question is the scripting equivalent of a candy hop: it looks like it might be a tough one to answer, but it turns out to be pretty easy.

WMI actually has a class – Win32_PNPEntity – that returns information about the devices found in Device Manager. Meanwhile, Win32_PNPEntity has a property – ConfigManagerErrorCode – that tells you whether or not the device is working properly. If the value of ConfigManagerErrorCode is 0, then the device appears to be working. If ConfigManagerErrorCode is anything but 0, then something must be wrong. And here’s the good part: any device with a ConfigManagerErrorCode value other than 0 will also have a little yellow exclamation mark next to it in Device Manager.

In other words, to write a script that returns a list of the devices that aren’t working properly we just need to connect to the Win32_PNPEntity class and query for all devices with a ConfigManagerErrrorCode other than 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 “Name: ” & objItem.Name Next

And what if you’d like more detailed information about all the devices installed on a computer? Well, because this question turned out to be an easy one to answer, we’re feeling generous: here’s a bonus script that returns all sorts of data about installed devices:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PnPEntity")
For Each objItem in colItems
    Wscript.Echo "Class GUID: " & objItem.ClassGuid
    Wscript.Echo "Device is Working: " & objItem.ConfigManagerErrorCode
    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

No, no charge for the bonus script. That one is on us!


Discussion is closed.

Feedback usabilla icon