Working with devices
I talked to several folks at Tech Ed who wanted to know if it was possible to work with devices and do something like an uninstall/reinstall using a Troubleshooting Pack. Yes, you absolutely can! In fact, there are two ways you can accomplish this, so I’ll show you both.
Method 1: Reuse the Windows “Devices” Troubleshooting Pack
This is definitely the method I’d recommend. It doesn’t take a lot of effort and you can get really good results. Windows 7 includes a great devices troubleshooter that can do much more than just uninstall and reinstall devices, and you’ll get all that extra goodness for free! Since it’s included with Windows 7 you know it’ll be there. All we need to do is tap into that workflow; let’s look at how you can do that.
If you don’t know which device is having problems you can just launch the device troubleshooter and it’ll try to figure it out. Launching this Troubleshooting Pack from the command line is simple, just type the following command. This is identical to launching the Troubleshooting Pack from the Troubleshooting Control Panel.
msdt.exe /id DeviceDiagnostic /skip true
If there is a specific device that’s causing you problems you can pass the PNPDeviceID for that device to the Troubleshooting Pack. To do this we must change the command line slightly. We’ve added the “/param” command line argument so we can pass in the specific PNPDeviceID of the problem device. Obviously, you need to know the PNPDeviceID for the problem device for this to be useful. This is identical to right-clicking a device in Devices and Printers and selecting Troubleshoot.
msdt.exe /id DeviceDiagnostic /param "IT_SelectDevice=[PNPDeviceID]" /skip true
msdt.exe /id DeviceDiagnostic /param "IT_SelectDevice=PCI\VEN_8086&DEV_27D8&SUBSYS_380217AA&REV_02\3&21436425&0&D8" /skip true
This is the best approach for general device issues. It’ll enable devices that have been disabled, search for drivers, and address several other potential problems.
Method 2: Build an Uninstall/Reinstall Troubleshooting Pack
The second way to uninstall and reinstall a device is to build your own Troubleshooting Pack. This is more work but will give you complete control over the experience.
The trick here is that while PowerShell gives you tons of great tools to find devices and check for problems, it doesn’t give you an obvious method to actually uninstall and reinstall the device. In an earlier post I showed you how to access Win32 APIs from PowerShell and that’s exactly what we need here. Coincidentally, the Win32 part of that example already has the code we need to reinstall a device, so all we need is some code to uninstall the device.
Rather than just post the uninstall/reinstall code, I thought I’d share with you a complete Troubleshooting Pack sample that’ll show you how to uninstall and reinstall a device. It enumerates all PCI devices and displays them in a list, so you can select the device to remove. You can also manually specify the PNPDeviceID of the device you want to work with. This sample requires the latest version of the Windows Troubleshooting Pack Designer, which you can download here. Unzip the sample into your “Documents\Troubleshooting Packs” folder and open it in the Troubleshooting Pack Designer.
So there you have it. Two ways to manage devices from within a Troubleshooting Pack. Pick the method that works best for your environment and end users. Drop me a comment if you find this useful or would like to see other examples.