Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell List Serial Ports + description

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)

like image 470
user3756570 Avatar asked Jun 19 '14 13:06

user3756570


People also ask

How do I read from a serial port in PowerShell v2?

For information about how to read from a serial port in an asynchronous manner in PowerShell V2, use the DataReceived event on the port object with the Register-ObjectEvent cmdlet.

How do I find a list of valid serial ports?

Use the GetPortNames method to query the current computer for a list of valid serial port names. For example, you can use this method to determine whether COM1 and COM2 are valid serial ports for the current computer. The port names are obtained from the system registry (for example, HKEY_LOCAL_MACHINEHARDWAREDEVICEMAPSERIALCOMM).

How do I view TCP ports in PowerShell?

As an Administrator, start a new POWERSHELL command-line prompt. List the current TCP connections. Here is the command output. List the open TCP ports. Here is the command output. In our example, we listed only TCP ports in the listening state. Optionally, use a filter to get only the desired columns.

What is the Order of the port names returned from getportnames?

The order of port names returned from GetPortNames is not specified. Use the GetPortNames method to query the current computer for a list of valid serial port names. For example, you can use this method to determine whether COM1 and COM2 are valid serial ports for the current computer.


3 Answers

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.

like image 144
Mark Stouffer Avatar answered Sep 20 '22 16:09

Mark Stouffer


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%"'
like image 23
Anthony Stringer Avatar answered Sep 17 '22 16:09

Anthony Stringer


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.

like image 32
Χpẘ Avatar answered Sep 20 '22 16:09

Χpẘ