获取 WMIObject powershell

Get-WMIObject powershell

我在 powershell 中有我的代码来打印 COM 端口

    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
}

这是结果:

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

我只是想让它显示这个:

DeviceID                                                                                             
--------                                                                                              
COM4              
COM3 

我怎样才能得到这个结果? 谢谢指教!

尝试通过以下代码更改第三行。

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