Remove Ghost devices natively with Powershell

Remove Ghost devices natively with Powershell

2017-06-28
/ /
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:

 

 

7 Comments

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

    Great work!

    Reply
  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
    }

    Reply
    • 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!

      Reply
  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

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

    Thanks, it works well and help me a lot !

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

    Thank you very much. Your script works really well.

    Reply
  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!

    Reply

Post a Comment

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.