2

I want to retrieve serial com port information as device manager shows. For example, "Intel(R) Active Management Technology - SOL (COM3)". I know use command "mode" to retrieve serial port list, but there is no description like "Intel(R) Active Management Technology - SOL". How do I get this information in windows batch file?

William Lai
  • 107
  • 1
  • 2
  • 10

2 Answers2

4

Updated answer

... was provided by the original asker in the comments below. Apparently Win32_SerialPort doesn't include USB->Serial devices. William's solution to this is to query Win32_PnPEntity instead.

Just so I still feel useful, here's a way to scrape that list and set each line into a variable:

@echo off
setlocal

:: wmic /format:list strips trailing spaces (at least for path win32_pnpentity)
for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| find "COM"') do (
    call :setCOM "%%~J"
)

:: display all _COM* variables
set _COM

:: end main batch
goto :EOF

:setCOM <WMIC_output_line>
:: sets _COM#=line
setlocal
set "str=%~1"
set "num=%str:*(COM=%"
set "num=%num:)=%"
set str=%str:(COM=&rem.%
endlocal & set "_COM%num%=%str%"
goto :EOF

Original answer

Use wmic with Win32_SerialPort, something like this:

@echo off
setlocal

for /f "delims=" %%I in ('wmic path Win32_SerialPort get DeviceID^,Caption^,Description^,Name^,ProviderType /format:list ^| find "="') do (
    set "%%I"
)

echo %DeviceID%
echo %Caption%
echo %Description%
echo %Name%
echo %ProviderType%

See the documentation for Win32_SerialPort to see what other properties you can query. I'll leave it as an exercise for you to find a way to set the variables uniquely so you aren't overwriting them with iterations over each COM port. Enjoy!

rojo
  • 24,000
  • 5
  • 55
  • 101
  • This script does not list devices such as a USB -> RS232 Virtual COM port. – Lynn Crumbling Jan 05 '15 at 13:31
  • @LynnCrumbling So far, the closest solution to that I've found is a PowerShell snippet on the [`Win32_USBControllerDevice` documentation page](http://msdn.microsoft.com/en-us/library/aa394505%28v=vs.85%29.aspx) as follows: `powershell -command "gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Sort Manufacturer,Description,DeviceID | Ft -GroupBy Manufacturer Description,Service,DeviceID"` (which probably needs the `%` double-pumped in a batch script). Not sure how to translate that to `wmic`, but the PowerShell cmdlets could probably be massaged to dial in the results you want. – rojo Jan 05 '15 at 16:55
  • @rojo, I find that maybe we can use Win32_PnPEntity: `wmic path win32_pnpentity get caption /format:table | find "COM"` – William Lai Jan 07 '15 at 06:22
  • Thanks, was a bit cryptic for a linuxer, yet easy enough to modify. For Batch noobs, such as myself, that `endlocal & ` part can be expanded as `endlocal & ( ; )` to multiple actions. Apparently, `endlocal` flushes local scope, however, that `& ...` part still has access to the scope and can smuggle variables out of the function/procedure. If one can cope with script-level variable side effects, it is not a necessity (put all script in a `setlocal ... endlocal` block to not leak to terminal). Still, just use the parenthesis. – cbugk Jul 24 '23 at 13:05
2

Please use mode command to get details of available COM ports..

Jerry James
  • 1,125
  • 1
  • 10
  • 17