13

I'd like to create a list of COM port on my computer (COM port + description). My point is to create a COM port list in order to communicate with a switch using an USB/RS232 converter.

What I try so far :

Get-WMIObject Win32_SerialPort | Select-Object DeviceID,Description

But all the COM port does not appear (example: COM11 is missing)

another attempt :

[System.IO.Ports.SerialPort]::getportnames()

here the port I need is present but the description is missing. (example : COM11 is present but with no details)

dsolimano
  • 8,870
  • 3
  • 48
  • 63
user3756570
  • 141
  • 1
  • 1
  • 3

4 Answers4

4

How about this?

$c1 = new-object System.IO.Ports.SerialPort com1
$c1
BaseStream             :
BaudRate               : 9600
BreakState             :
BytesToWrite           :
BytesToRead            :
CDHolding              :
CtsHolding             :
DataBits               : 8
DiscardNull            : False
DsrHolding             :
DtrEnable              : False
Encoding               : System.Text.ASCIIEncoding
Handshake              : None
IsOpen                 : False
NewLine                :

Parity                 : None
ParityReplace          : 63
PortName               : com1
ReadBufferSize         : 4096
ReadTimeout            : -1
ReceivedBytesThreshold : 1
RtsEnable              : False
StopBits               : One
WriteBufferSize        : 2048
WriteTimeout           : -1
Site                   :
Container              :

You could do this for each port that comes back from getportnames(). You'll probably want to call the Dispose() method on each port and set $c1 to $null after you finish gathering info for it.

Χpẘ
  • 3,403
  • 1
  • 13
  • 22
  • 1
    For the sake of completion for a newbie who doesn't understand how to put it all together, here is my combo: `$COMportList = [System.IO.Ports.SerialPort]::getportnames()` `ForEach ($COMport in $COMportList)` `{` `$temp = new-object System.IO.Ports.SerialPort $COMport` `echo $temp.PortName` `echo $temp.BaudRate` `$temp.Dispose()` `}` – PeterFnet Mar 22 '17 at 02:24
  • 1
    This answer doesn't seem to work any longer for PowerShell 5.1.19041.906. @Mark Stouffer's answer works. – ndemarco Apr 27 '21 at 16:58
4

The above answers seem to be for deprecated Powershell objects.

I was able to use this:

Get-CimInstance -Class Win32_SerialPort | Select-Object Name, Description, DeviceID

Remove | Select-Object Name, Description, DevideID to inspect additional properties.

Mark Stouffer
  • 97
  • 2
  • 10
3

did this:

https://www.google.com/search?q=powershell+get+available+com+ports&gws_rd=ssl

found this:

http://empegbbs.com/ubbthreads.php/topics/362862/Windows_command_to_quickly_lis

which led to this:

https://github.com/todbot/usbSearch/blob/master/listComPorts.vbs

so i adapted it to this:

Get-WmiObject Win32_PnPEntity -Filter "Name LIKE 'com%'" | Where Name -match 'COM\d+'

or this

Get-WmiObject -Query 'SELECT Name, Description from Win32_PnPEntity WHERE Name LIKE "com%"'
Anthony Stringer
  • 1,981
  • 1
  • 10
  • 15
0

If you really wanna see all the good details, you should cook up something like the following:

    $mydevs = (Get-PnPDevice | Where-Object{$_.PNPClass -in  "WPD","AndroidUsbDeviceClass","Modem","Ports" } | 
        Where-Object{$_.Present -in "True"} | 
        Select-Object Name,Description,Manufacturer,PNPClass,Service,Present,Status,DeviceID | 
        Sort-Object Name)


    $mydevs | Format-Table Description, Manufacturer, PNPClass, Service,
        @{Label="COM port"; Expression={ ($_.Name -Match "\((COM\d{1,2})\)" | Out-Null && $Matches[1]) }},
        @{Label="VID:PID"; Expression={ ($_.DeviceID -Match "USB\\VID_([0-9a-fA-F]{4})\&PID_([0-9a-fA-F]{4})" | Out-Null && ('{0}{1}{2}' -f ${Matches}[1], ":", ${Matches}[2]).ToLower() ) }},
        Present, Status

The output is:

Description                                 Manufacturer         PNPClass Service   COM port VID:PID   Present Status
-----------                                 ------------         -------- -------   -------- -------   ------- ------
Intel(R) Active Management Technology - SOL Intel                Ports    Serial    COM7                  True OK
USB Serial Device                           Microsoft            Ports    usbser    COM4     8087:0aca    True OK
USB Serial Device                           Microsoft            Ports    usbser    COM6     8087:0aca    True OK

From my answer here.

not2qubit
  • 14,531
  • 8
  • 95
  • 135