Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#352184 - 16/05/2012 16:25 Windows command to quickly list the available COM ports on a system?
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
I'm having trouble finding out how to do this particular thing. Can anyone help?

I want to use a Windows command prompt to enumerate, either to the console or to a text file, the available COM ports on the computer. I need the list to include the detailed description of the devices so that I can tell which ports belong to which pieces of plugged-in USB hardware. For instance, an Arduino Uno will have its name in the description along with the COM port number, or a USB GPS will say its brand and model in the description along with its COM port number.

Essentially, I want to do what this does, but do it from a Windows command prompt.

The following command works:
msinfo32 /categories +Components /report report.txt
... but it takes forever to run, and the resulting file would take a ton of parsing to find the tiny thing I'm looking for.

Does anyone know if there is something simpler?
_________________________
Tony Fabris

Top
#352186 - 16/05/2012 17:00 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
C:\>wmic path Win32_SerialPort

OR

C:\>powershell
PS> Get-WMIObject Win32_SerialPort

OR

PS> Get-WMIObject Win32_SerialPort | Select-Object Name,DeviceID,Description

...which is the exact equivalent of the accepted answer on that question.

In short: use PowerShell; it rocks.
_________________________
-- roger

Top
#352187 - 16/05/2012 17:05 Re: Windows command to quickly list the available COM ports on a system? [Re: Roger]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Originally Posted By: Roger
In short: use PowerShell; it rocks.

Top
#352193 - 16/05/2012 18:21 Re: Windows command to quickly list the available COM ports on a system? [Re: drakino]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Originally Posted By: drakino
Originally Posted By: Roger
In short: use PowerShell; it rocks.
_________________________
Bitt Faulk

Top
#352202 - 16/05/2012 19:50 Re: Windows command to quickly list the available COM ports on a system? [Re: Roger]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
Aha, that's perfect, Roger!

Digging more, I find out I can also filter results I'm searching for with the WMIC SQL Query Language, such as:

wmic path Win32_SerialPort Where "Caption LIKE '%Arduino%'" Get DeviceID
wmic path Win32_SerialPort Where "Caption LIKE '%GPS%'" Get DeviceID

Those will return the exact COM port ID of the things I'm looking for. FANTASTIC! Thank you!
_________________________
Tony Fabris

Top
#352203 - 16/05/2012 20:16 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
And the full DOS command to put the query into a variable that I can use elsewhere in the batch file is:

Code:
for /f "usebackq" %%B in (`wmic path Win32_SerialPort Where "Caption LIKE '%%Arduino%%'" Get DeviceID ^| FIND "COM"`) do set comport=%%B
echo Com Port for Arudino device is detected as %comport%.
_________________________
Tony Fabris

Top
#352223 - 17/05/2012 03:50 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
Originally Posted By: tfabris
And the full DOS command to put the query into a variable that I can use elsewhere in the batch file is


...still better in PowerShell:

Code:
$port = ( Get-WmiObject Win32_SerialPort | Where { $_.Description -like '*Arduino*' } | select -first 1 )
_________________________
-- roger

Top
#358543 - 06/05/2013 21:12 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
You know how, when you search for how to accomplish a task on the internet, something you're sure you've never done before, and the google result points you to a thread where you asked that question on the EmpegBBS, and had a working successful answer?

Me neither.
_________________________
Tony Fabris

Top
#358547 - 07/05/2013 11:20 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
Note to self: There's a nice VBScript version here:

http://github.com/todbot/usbSearch

This is useful because it indexes all plugnplay items on the system with COM(x) in the name, which means it gets all com ports and faux com ports, not just the "real" com ports.

(He's got a bug in it though. The Regex throws an exception if the string is null so you should put an IF around it.)
_________________________
Tony Fabris

Top
#362852 - 31/10/2014 08:44 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
freddan1
new poster

Registered: 31/10/2014
Posts: 1
Hi,
Very useful tips! Thank you very much!
Both ways works really good but I decided to use tfabris way of finding the comport.
There is a problem to it. I cant use the full expression if Im not Admin.
I get problems when I add FIND "COM" .
The program i make will be out of my hands in future. I cant garantee that is will run on a computer with admin rights.
Is there a work around or something I can do to make it work as a non Admin??
Thanks!


Edited by freddan1 (31/10/2014 11:13)

Top
#362856 - 31/10/2014 16:23 Re: Windows command to quickly list the available COM ports on a system? [Re: freddan1]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
I am surprised to hear that piping into the FIND command requires admin privileges, I would not have expected that, but stranger things have happened. I would suggest that you doublecheck that that's really the issue, and that it's not perhaps something else that maybe you can work around.

To truly work around admin issues, I'd recommend trying the PowerShell and VBScript variants mentioned elsewhere in the thread. Those might work even without administrative permissions, or can be made to work sans-admin with some additional code.

Another option would be to check for admin privileges before starting the script, and then refuse to run if the privileges aren't there. My favorite way to check for admin privileges in a DOS batch file is this one:

Code:
@echo off

:: -------------------------------------------------------
::                  MAIN PROGRAM START
:: -------------------------------------------------------

    echo  Hello, I am a batch file. I need administrative permissions
    echo  in order to run correctly. I am going to check for those
    echo  permissions now.

    pause

    call :check_Permissions


    echo  If we have reached this point, it means permissions were
    echo  good. I am able to continue this batch file.
    

::           REST OF MAIN PROGRAM GOES HERE    

::           ...
::           ...


:: -------------------------------------------------------
::                  MAIN PROGRAM END
:: -------------------------------------------------------

    goto :VeryEnd


:: -------------------------------------------------------
::                    SUBROUTINES
:: -------------------------------------------------------

:check_Permissions
    net session >nul 2>&1
    if %errorLevel% NEQ 0 (
         Echo        ================================================
         Echo        =   ERROR    ERROR    ERROR    ERROR    ERROR  =
         Echo        ================================================
         Echo      Program is not running with administrative permissions.
         Echo:
         pause
         exit
    )
goto :eof


:VeryEnd
_________________________
Tony Fabris

Top
#362860 - 31/10/2014 18:31 Re: Windows command to quickly list the available COM ports on a system? [Re: tfabris]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
Originally Posted By: tfabris
My favorite way to check for admin privileges in a DOS batch file is this one


Still easier in PowerShell:

Code:
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
        [Security.Principal.WindowsBuiltInRole] "Administrator")


smile
_________________________
-- roger

Top
#362861 - 31/10/2014 19:34 Re: Windows command to quickly list the available COM ports on a system? [Re: Roger]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5549
Loc: Ajijic, Mexico
Originally Posted By: Roger
Still easier in PowerShell:
Yeah, but nowhere near as much fun. smile

tanstaafl.
_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#362862 - 31/10/2014 19:42 Re: Windows command to quickly list the available COM ports on a system? [Re: Roger]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31601
Loc: Seattle, WA
Originally Posted By: Roger
Still easier in PowerShell:
Code:
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
        [Security.Principal.WindowsBuiltInRole] "Administrator")



I haven't looked back at that one for a while, but I seem to recall that the method you list there didn't work the way I needed it to work. I might be remembering it wrong, but here was the issue I encountered...

- On Windows 7 with UAC disabled, simply checking to see if your program was running as a member of the builtin administrators role was enough.

- On Windows 8 and later, even if you disable UAC, running a program under the builtin administrator account still was not enough. For example if you run a DOS batch file or any EXE, it is, by default, not set to have administrator-level privileges even if you are already logged in as a member of the administrators group, so you will still get permissions errors. The only way around it is if you explicity right-click and select "run as administrator" on the batch file or EXE. (Or if the EXE is written to self-elevate.)

I seem to recall that the method you quote there won't accurately determine the latter thing. (As I said I could be remembering it wrong.)
_________________________
Tony Fabris

Top