Wednesday, May 4, 2011

Windows Hidden/Not Connected Device Removal Script And Devcon Musings

I recently needed to create a quick script that uninstalls any devices that have since been disconnected from the system (or fail to be seen). Normally these devices can only be found if you open a command prompt and enter the following (then show hidden devices):

set devmgr_show_nonpresent_devices=1
start devmgmt.msc

The problem arose when I had a large number of touchy USB data aquisition devices that required this to be performed regularly, which becomes very tedious. This is where the devcon utility comes in handy. Although there are a few small problems; the devcon utility has issues removing hidden devices. A lot of people claim this is because it just in fact doesn't do it, as there is apparenly a flag in the program that prevents you from doing it out of the box and modifying the source is the only way to go (I have looked through the source, and I could find no such thing that would cause this behaviour). This is flat out incorrect, you can indeed remove hidden devices with devcon out of the box (provided you have the correct version for your Windows installation, more on this later).

Firsly to do this you need the full device ID string that devcon spits out from a find command. This string will look something like this: 

PCI\VEN_1000&DEV_0060&SUBSYS_1F0C1028&REV_04\4&10333E29&0&0030. 

You can then remove the device with the following command:

devcon remove
"@PCI\VEN_1000&DEV_0060&SUBSYS_1F0C1028&REV_04\4&10333E29&0&0030"


Take note of the quotes and @ symbol as these are required to allow hidden devices to be removed. Once we have this information, we can make our lives easier by creating a script to remove all hidden devices from a machine:

@ECHO OFF
DEVCON Find * | FIND /I /V "matching device(s)" > "DevicesExist.txt"

FOR /F "tokens=1 delims=: " %%A IN ('DEVCON FindAll * ^| FIND /I /V "matching device(s)"') DO (
        TYPE "DevicesExist.txt" | FIND "%%~A" >NUL
       IF ERRORLEVEL 1 %Debug% DEVCON Remove "@%%~A"
)


This script finds all devices that aren't hidden, and then compares that to a list of every single device seen and removes those that are no longer connected.

Save the above as a batch file and run it in the same directory as devcon and you are done. Now I mentioned earlier that you will need the correct version of devcon for your installation of Windows. If you find that the above script does not work for you (you will probably get the error: ‘Remove failed. No devices removed.’), you will then need to recompile devcon from the sources in the Windows Driver Kit for the operating system you are going to run on. Don't worry, it's not as hard as you might think. Install the Windows Driver Kit and then goto Start, Programs, Sevelopment Kits, WDK XXXX, Build Environments, Windows [Target], Windows [Target] Free Build Environment, replacing [Target] with the appropriate version of Windows you are building for. Now goto the src\setup\devcon folder in the WDK install directory and run:

build -ceZ

You can now use the newly built devcon with the above script.

3 comments: