Error using powershell.exe: 'Select-Object' is not recognized

23 views (last 30 days)
I want to get information about serial ports via the powershell.
I got the results i want, when I write some line of code in command Window:
system("powershell")
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\> Get-CimInstance -Class Win32_SerialPort | Select-Object Name, Description, DeviceID, PNPDeviceID
Get-CimInstance -Class Win32_SerialPort | Select-Object Name, Description, DeviceID, PNPDeviceID
Name Description DeviceID PNPDeviceID
---- ----------- -------- -----------
Arduino Uno (COM4) Arduino Uno COM4 USB\VID_2341&PID_0043\55737313331351A00212
Arduino Micro (COM38) Arduino Micro COM38 USB\VID_2341&PID_8037&MI_00\7&2D66C3B6&0&0000
PS C:\Users\> exit
But when I run the same commands in a single line or as function, i get an error:
>> system("powershell Get-CimInstance -Class Win32_SerialPort | Select-Object Name, Description, DeviceID, PNPDeviceID")
'Select-Object' is not recognized as an internal or external command,
operable program or batch file.
I think that the second part of the command (after the | ) is not executed in the powershell but in the cmd. How can I solve this?
  1 Comment
inl1ner
inl1ner on 2 Nov 2023
In PowerShell funktioniert es so:
$GetPort = Get-CimInstance -ClassName Win32_PNPEntity |
Select-Object -Property Caption, $StatusInfo
Write-Host $GetPort
@{Name=Kommunikationsanschluss (COM1); Description=Kommunikationsanschluss; DeviceID=COM1; PNPDeviceID=ACPI\PNP0501\0}

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!