Remove Ghost devices natively with Powershell

Remove Ghost devices natively with Powershell

/ /
in Blog

We’ve been looking at using Base Image Scripting Framework (BISF) as our new preparation and personalization platform for our PVS farm.  In our current world we have a bunch of features and tweaks that are not apart of BISF so I’ve been writing some additions so that we achieve feature parity.

One of the features I wanted to take a good hard look at was removing ‘Ghost’ devices — or devices that aren’t present in your system.  Ghost devices look like this:

Our current world accomplishes this task by using a script written by Simon Price and devcon.exe.  There is nothing wrong with this method per se but BISF is native Powershell and I want to stick to that without having outside dependencies.  Can this requirement be achieved with nothing but PowerShell?  Fortunately, google came to rescue and pointed me here.  A script written by Alexander Boersch got me 80% of the way there (whoo hoo!).  He wrote the method and ability to access setupapi.dll which gives us the functions and methods necessary to query and manipulate devices in Device Manager.  PowerShell is supposed to have the ability to do C# code natively and his example was perfect for taking me where I needed to go.

How does it work?  What’s the output?



notice the “filter match” text



And a brief video of it in action:


Lastly, the script:




  1. Greg Wood 2017-10-20 10:21 am

    Great work!

  2. dave 2018-03-30 12:32 pm

    I just found this and wanted to say thanks! We use a content management app here for scanning, and it’s picky. Every time the Fuji scanners we use get plugged into a different USB port, windows shows an active scanner, and a false/hidden scanner. I modified this to include a parameter for just sorting through scanners so it will delete *all* scanners if it sees any false/hidden scan devices.

    I also added this, because it complains if you run it on a system more than once — not your intent, but it is likely to happen where i work.

    if (-not ([System.Management.Automation.PSTypeName]’Win32.SetupApi’).Type){
    Add-Type -TypeDefinition $setupapi


Post a Comment

Your email address will not be published. Required fields are marked *