I want to incorporate a dropdown menu that is populated with the list of available COM ports. I can't find any way to easily get the names of the available COM ports to put in the place of COM4 that creates the $port.
$port = new-Object System.IO.Ports.SerialPort COM4,19200,None,8,one
By using Win32_SerialPort I am able to easily extract COM1 and COM3.
Get-WmiObject Win32_SerialPort | Select-Object deviceid
Results:
deviceid
COM3
COM1
But my device manager shows 16 available ports from a remote serial hub.
Device Manager Snapshot
Here is what I have tried and I am able to narrow down the Name, but can't figure out how to extract just the (COM–) part.
Get-WmiObject Win32_pnpentity -Filter "Name LIKE 'devicemaster port%'" | Select-Object -Property Name
Best Answer
Adding a late answer because I just had a need for this...
You can use WMI ClassGuids to get the exact list (COM and LPT) that device manager shows:
Confirmed to work with a few LPT / COM extension cards (Brain Boxes / Exar), using Windows 8.1 up to server 2019 (Powershell 4 onwards).
The full list of ClassGuids is here: https://docs.microsoft.com/en-us/windows-hardware/drivers/install/system-defined-device-setup-classes-available-to-vendors