none
Script to Uninstall Nonpresent USB devices from device manager

    Question

  • Hi all,

    I'm new to this forum (and powershell in general) and created this account just to see if I can get an answer to this question. I'm running Windows 7 Professional and need a script to uninstall all nonpresent USB mass storage devices from device manager (and possibly some other devices). I tried using the Devcon.exe utility, but found that it can't uninstall nonpresent devices (only present ones). Essentially, I'm trying to find a way to emulate device manager's uninstall utility for nonpresent devices. Is there any way to uninstall these devices with a script? I'm hoping to deploy this to a larger environment eventually.

    I've looked at this page http://blogs.technet.com/b/wincat/archive/2012/09/06/device-management-powershell-cmdlets-sample-an-introduction.aspx and many others like it, but none seem to be able to actually uninstall a nonpresent device. (pnputil.exe doesn't seem to update the registry properly, which leads to the devices reappearing in device manager)

    Any help at all would be appreciated, and I apologize if I've missed something obvious in my search for an answer to this question.

    Thanks!

    Sam S.

    • Moved by Bill_Stewart Wednesday, May 13, 2015 3:49 PM Move to more appropriate forum
    Wednesday, May 13, 2015 2:46 PM

Answers

  • Nevermind. I found a utility that does what I needed. It's called USBDeview and is available here: http://www.nirsoft.net/utils/usb_devices_view.html

    Anandtech forums found me that.

    • Marked as answer by S Swanson Thursday, May 14, 2015 7:13 PM
    Thursday, May 14, 2015 6:45 PM

All replies

  • These devices are not installed they are just recorded in the registry.  The next time you plug the device in it will obtain the same ID as the previous time.  If you remove the registry entry it will get created with a new ID which may not be what is expected.

    Do not worry about these entries in the registry.  They do no harm.

    If drivers that support these devices are installed don't worry either.  They are only loaded when the device is attached.

    Spend some time researching how USB is intended to work.  Learn how Windows supports the USB specification.  There is little you need to do.


    \_(ツ)_/

    Wednesday, May 13, 2015 3:10 PM
  • I suppose I should have clarified this a bit. I need to uninstall these devices and ensure their drivers and registry entries are deleted to allow all future USB devices to be filtered with a GPO.

    The specific GPOs are "Allow installation of devices that match any of these device IDs" and "prevent installation of devices not described by other policies" under

    computer config>admin templates>system>device installation>restrictions

    As such, the entries in the registry actually DO harm this task, and I need to be sure that a previously installed device will NOT retain its installation drivers and registry entries so that the GPO can actually block its installation.

    Wednesday, May 13, 2015 3:20 PM
  • You miss the point of the GPO.  Once set it will override any devices already installed.  You do not need to change anything,

    Most of the USB drivers come pre-installed in Windows.  They are numerous.  You would hyave to walk through every driver in the system and track down whether it is needed or not.

    You can write a script to do this but it is not a short script.

    This is also unnecessary.


    \_(ツ)_/

    Wednesday, May 13, 2015 3:24 PM
  • I have tried the GPO, and previously installed USB devices that I do not allow will still work because they are already installed. I've tried this, and that's how it works. Maybe there are other GPOs that work differently that I don't know about, but this is the one I'm using right now (if you know of a GPO that would work better, please tell me what it is).

    I've gone through the device manager GUI and uninstalled these devices and then activated the GPO, and it blocks their new installation. If I do not uninstall them, they are still allowed (even though I haven't specifically allowed them).

    Maybe I was on the wrong track with the registry and drivers and whatnot, but I still need to know if there is a way to script the uninstallation of nonpresent devices the same way that device manager uninstalls them (as well as how to script this. I don't need the whole script, just a starting point please).

    Thanks again.

    Why did this get moved to the group policy section? This is about scripting.
    • Edited by S Swanson Wednesday, May 13, 2015 4:22 PM
    Wednesday, May 13, 2015 3:39 PM
  • Just use a GPO to remove all registry entries under the USB device list. 

    \_(ツ)_/

    Wednesday, May 13, 2015 4:46 PM
  • And how would I go about doing that?
    Wednesday, May 13, 2015 5:50 PM
  • Post in GP forum.  They will show you how to manage registry from GP.

    You can start by looking here:  GCI hklm:SYSTEM\CurrentControlSet\Enum\USBSTOR -recurse


    \_(ツ)_/

    Wednesday, May 13, 2015 6:32 PM
  • Nevermind. I found a utility that does what I needed. It's called USBDeview and is available here: http://www.nirsoft.net/utils/usb_devices_view.html

    Anandtech forums found me that.

    • Marked as answer by S Swanson Thursday, May 14, 2015 7:13 PM
    Thursday, May 14, 2015 6:45 PM
  • Just a heads up on this, it can cause problems sometimes with some storage devices. When the storage device is removed and re-connected sometimes there is trouble with is being detected. The only way I have found to resolve this issue is to remove the "nonpresent" device and plug it back in.
    Friday, April 08, 2016 1:16 PM