Search code examples
powershellprintingbluetoothcomwmi

Get-WMIObject powershell


i have my code in powershell to print the COM port

    Set-ExecutionPolicy Bypass -Scope CurrentUser
try {
Get-WMIObject Win32_SerialPort | Select-Object DeviceID, PNPDeviceID | Where-Object{$_.PNPDeviceID -like "*$env:asset*"} 
}
catch{
    Write-Warning "Nessuna porta COM trovata, assicurarsi che il dispositivo sia correttamente collegato"
    $LASTEXITCODE = 1
    exit $LASTEXITCODE
}

this is the results:

DeviceID PNPDeviceID                                                                                             
-------- -----------                                                                                             
COM4     BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&2A34F9CE&0&000000000000_00000006         
COM3     BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_VID&000101E6_PID&1128\7&2A34F9CE&0&000780F5F461_C00000000

I just want it to show this:

DeviceID                                                                                             
--------                                                                                              
COM4              
COM3 

How can i obtain this result? Thanks in advice!


Solution

  • Try by Changing the third Line by below code.

    (Get-WMIObject Win32_SerialPort |
        Select-Object DeviceID, PNPDeviceID |
        Where-Object{$_.PNPDeviceID -like "*$env:asset*"}).DeviceID