Mr. Script

Device Management

The command-line tool DevCon.exe provides a ton of functionality.

One of the best things about scripting is taking control away from the Windows interface. There's nothing finer than finding some hidden API that exposes the features of an oft-employed GUI tool. Unfortunately, Microsoft doesn't always give their tools an API that we can script against. Case in point: Device Manager. While WMI gives us some power over our physical devices, we don't have an API that provides Device Manager's powerful capabilities. Nevertheless, (spoken in my best sinister accent) "ve haf vays of makink it vork!" Microsoft has a command-line tool called DevCon.exe that you can use to enable, disable, restart, update, remove and query devices or groups of devices. Wrap that up with the WScript.Shell object and you have something you can really script with. Alternatively, you can just put it into a command-line script (.cmd file), but where's the fun in that?

To get started using this fascinating utility, you must first download it from here. (Note: Microsoft states that DevCon is unsupported and not redistributable. It's intended for use as a debugging and development tool, so you're on your own. 'Nuff said.)

This page also contains some information to help you use DevCon—which is great, because it doesn't have an accompanying help file. Now that you've downloaded it, let's take a look at what it can do.

Listing 1

C:\Devcon\i386>devcon /?
Device Console Help:
devcon [-r] [-m:\\] [...]
-r if specified will reboot machine after command is complete, if needed.
is name of target machine.
is command to perform (see below).
... is one or more arguments if required by command.
For help on a specific command, type: devcon help

classfilter
Allows modification of class filters.
classes
List all device setup classes.
disable
Disable devices that match the specific hardware or instance ID.
driverfiles
List driver files installed for devices.
drivernodes
Lists all the driver nodes of devices.
enable
Enable devices that match the specific hardware or instance ID.
find
Find devices that match the specific hardware or instance ID.
findall
Find devices including those that are not present.
help
Display this information.
hwids
Lists hardware IDs of devices.
install
Manually install a device.
listclass
List all devices for a setup class.
reboot
Reboot local machine
remove
Remove devices that match the specific hardware or instance ID.
rescan
Scan for new hardware.
resources
Lists hardware resources of devices.
restart
Restart devices that match the specific hardware or instance ID.
sethwid
Modify Hardware IDs of listed root-enumerated devices.
stack
Lists expected driver stack of devices.

status

List running status of devices.
update
Manually update a device.
updateni
Manually update a device (non interactive).

Listing 1 shows all of DevCon's commands. By specifying the machine where you want to run the command (DevCon uses Interprocess communication—IPC—to access remote computers) and the necessary arguments, you can get DevCon to perform a host of hardware-related operations. Scroll down to the "More Information" section of this page to find some examples of how to use these commands.

Where Do I Begin?
As you can see, DevCon.exe provides a lot of functionality. In order to get a handle on its capabilities, you'll need to play with it for a while. After all, it took you more than just a couple of minutes to get familiar with Device Manager, didn't it? And that's a GUI tool! Be prepared to invest some time in learning how to use DevCon. It will be time well spent.

One suggestion: I'd recommend that you begin by working with the enable and disable commands—specifically as they relate to network interface cards.

There. I just gave you a big clue as to what we will be discussing next month. Geez! It's like having homework all over again, isn't it? And I was doing so well avoiding that lately.

More Information

Examples for Using DevCon.exe

This first example lists the instances of all devices on the local computer. You might want to pipe that into a text file for easier viewing; my computer, for example, had 177 listed.

From the command prompt:

C:\Devcon\i386>devcon find *

From a script:

Set objShell=CreateObject ("WScript.Shell")
objShell.Run "cmd /c devcon find *"

This next example displays the status of each PCI instance.

Command prompt:

C:\Devcon\i386>devcon status @pci\*

Script:

Set objShell=CreateObject ("WScript.Shell")
objShell.Run "cmd /c devcon status @pci\*"

Here's a real gem:

Command prompt:

C:\Devcon\i386>devcon update mydev.inf *pnp0501

Script:

Set objShell=CreateObject ("WScript.Shell")
objShell.Run "cmd /c devcon update mydev.inf *pnp0501"

The above command updates all devices that match hardware ID *pnp0501 with the best associated driver in mydev.inf. Sure beats drilling through the GUI!

To make it even more script-friendly, DevCon provides a return value to the script that indicates the status:
"0" = Success
"1" = Restart Required
"2" = Failure
"3" = Syntax Error

Of course, in order to get the return value, we have to assign it to a variable when we call the Run method of the Shell object:

Set objShell=CreateObject ("WScript.Shell")
iResult=objShell.Run("cmd /c devcon update mydev.inf *pnp0501")
WScript.Echo iResult

About the Author

Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].

Featured

comments powered by Disqus

Subscribe on YouTube

Upcoming Training Events

0 AM
TechMentor @ Microsoft HQ
August 11-15, 2025