locked
Can PowerShell be used to delete hidden USB/COM Ports? RRS feed

  • Question

  • If you go to Device Manager and display the Hidden Devices, you will see devices that were connected at one time, but are no longer.  Windows still keeps these COM ports allocated.

    In PowerShell, I can use Get-PnpDevice -Class "Ports" to list all the ports.  The hidden ports will be shown as Unknown.

    I need to be able to delete these hidden ports programmatically.  So, I thought I could call a PS script from C# (which I know how to do).  I just need to know if there is a script command to delete the hidden ports that are not in use.

    Thanks.

    Monday, January 27, 2020 8:38 PM

Answers

All replies

  • Doesn't make any sense. Ports are defined by the driver when it detects a device. To delete the ports you would have to remove the device.  This can only be done using external utilities or via the GUI.


    \_(ツ)_/

    Monday, January 27, 2020 11:43 PM
  • I know you can delete the hidden devices from the Device Manager GUI.  I also found there is some freeware that will do this also.  If someone wrote freeware to do it, it should be possible to do this programmatically.  Since I haven't found out how yet, I was going to try PowerShell.

    You are saying there is no way to do this with a PowerShell script if I specify to COM Ports that are marked Unknown when I use Get-PnpDevice -Class "Ports"

    I would include a screenshot, but the forum will not let me.

    Thank you for responding.

    Tuesday, January 28, 2020 12:23 AM
  • THere is no cpommand in PowerShell to delete a device.  You can uuse thrird party apps but PS would require writing compileed code that can be qcalled from PowerShell.  By default PS is not able to call teh Win32 API.  It requires that you compile a bit of code to do this.

    Start by reading all of this:

    help add-type -online

    Also this will set you on the right path.

    https://docs.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke

    https://www.leeholmes.com/blog/2009/01/19/powershell-pinvoke-walkthrough/


    \_(ツ)_/

    • Marked as answer by gridman694 Tuesday, January 28, 2020 1:54 AM
    Tuesday, January 28, 2020 1:48 AM
  • I think what you're talking about is like something I just found.  There is a post with a PS script that removes all ghost devices.  It has a bit of C# code in it.  I used it to just output the Ghost Devices, but not remove them, just to see what it would do, and it does work.  I would probably need to pare it down as it does more than I need.

    It won't allow me to post the link, but it's on a blog called TrententTye and the title of the post is "Remove Ghost devices natively with Powershell"

    I will also check out the links you posted.  Thanks for the help.
    Tuesday, January 28, 2020 2:06 AM