Reputation: 161
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)
Upvotes: 15
Views: 42499
Reputation: 16912
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.
Upvotes: 2
Reputation: 107
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.
Upvotes: 5
Reputation: 2001
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%"'
Upvotes: 3
Reputation: 3451
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.
Upvotes: 4