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

    • Tyson 2018-06-21 7:29 pm

      @dave, Did you just add this to the end of the script (the piece for running on a system more than once)? I want to do the same but make sure I put it in the right place. Thanks!

  3. Sebastian 2018-05-24 12:51 am

    I also like to say thank you because it helps me a lot.
    I had a server with around 80.930 ghost devices. Remove it manual was not possible. The script ran for about 3 days and successfully removed all ghost devices.
    That saved me the week.

    Good Work

  4. wilLiam 2018-06-14 9:30 am

    Thanks, it works well and help me a lot !

  5. Bob S 2018-10-19 9:20 am

    Thank you very much. Your script works really well.

  6. TomD 2018-10-25 3:22 pm

    This is a very well written PS script but what I need to do is simply remove three or four ghost printers from many computers in our organization. To “except” everything but the printers in question seems impractical. A function that would INCLUDE or EXCEPT filter results would be ideal. An additional parameter such as /i or /e with -filterByFriendlyName would allow one to remove one or more ghost devices or remove ALL BUT one or two ghost devices. That would seem to be more of a useful tool unless I’m missing something.
    This script is far too sophisticated for me to modify it for my needs . . . way above my pay grade!

  7. Eric Christensen 2018-11-29 12:29 pm

    Beautimus! Exactly what I was looking for.

    Thank you!

  8. Matthew Lewallen 2019-02-08 9:24 pm

    Very helpful, thank-you!

  9. István Siroki 2019-06-12 5:31 am

    Fantastic work, thank you!

  10. István Siroki 2019-06-16 9:16 am

    I’ve created an updated version of this script which can be accessed here: TrententTye, if this script is available somewhere where I can submit my change, please let me know.

  11. RV 2019-11-08 7:49 am

    Thanks for the script Istvan, but I ran into a issue after running the script:
    The script removes the “Microsoft Virtual Network Adapter” and then sets the already defined static IP to DHCP, however if I do ipconfig /all on the command prompt, I see the “DHCP Enabled = No”. Not sure how to fix this.

  12. Marcel 2019-12-23 6:00 am

    Works like a charm on first run!
    Thanks a lot!

  13. Pedro Röseler 2020-01-29 5:39 am

    Great Script!!!
    Thanks for sharing with us!


  14. Luis A Orande 2020-04-02 3:40 am

    Just found your script and looking forward to test it.

  15. Luis 2020-04-02 2:37 pm

    Awesome work!!!


Leave a Reply to TomD Cancel reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.