Removing Hidden Devices from VMs

I've been tasked with coming up with a script that will remove hidden devices from VMs. Anyone who's used VMs before knows that sometimes "ghost" devices show up. We've had a few problems recently and would like to write a script that will, on startup, remove any of these hidden devices (yes, I'm fully aware some may be needed, we have logic that will not nuke them all).

Say I have an array that contains devices in WMI type format, let's call that list "$devices"

How would I remove each one using powershell? I've tried devcon, but I feel like I'm not getting the syntax right, my script is doing the following:

foreach ($device in $devices){
    $toremove = $device.name
    c:\windows\system32\devcon.exe remove $toremove
    }

Is there a better tool than devcon, like maybe a PowerShell cmdlet (even one I have to download from somewhere)? If not, could someone help me get my syntax right (or tell me I need to use invoke- ?)

Thanks in advance.

February 3rd, 2015 4:31pm

I believe you can option Windows to not remember removable devices and then you won't get "ghost" devices.  Windows remembers every removable device so it can grab the same drive letter among other things.
Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2015 5:04pm

Not sure it will help us in this case, as we're forced to go off a base image that for compliance reasons can't change. We really just need to be able to pull some of those devices out.
February 3rd, 2015 5:13pm

devcon expects some kind of hardware ID, so I think passing the device name may not be working. I found this post which details using devcon to disable and enable a camera device. It uses Get-CimInstance Win32_PnPEntity and the pnpDeviceID as the parameter to devcon.

There is also a PowerShell Module with Cmdlets for managing devices, which you might find useful.

Free Windows Admin Tool Kit Click here and download it now
February 4th, 2015 11:25am

Hi Zarberg,

Im writing to just check in to see if the suggestions were helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up.

If you have any feedback on our support, please click here.

Best Regards,

Anna Wang

TechNet Community Support

February 11th, 2015 5:01am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics