PowerShell: playing around with Get-PnpDevice filtering with -Class and -Status
Posted by jpluimers on 2025/07/29
I while ago I was playing around in PowerShell with Get-PnpDevice (which got introduced in Windows 10 and Windows Server 2019):
[Wayback/Archive] Jeroen Wiert Pluimers: “@jilles_com … this is the difference between only connected disks versus including ones that had been connected in the past.Output difference between Get-PnpDevice -Class DiskDrive -Status OK Get-PnpDevice -Class DiskDrive …” – Mastodon
It kind of is the successor to both below when querying for Win32_PnPEntity instances:
Get-WmiObjectGet-CimInstance
I kind of like it more as it allows for:
- filtering during query (using parameters like
-Class,-FriendlyNameand-Status) instead of after the query (usingWhere-Object) - can query both connected and disconnected devices (based on either the
Statuscolumn by passing the-Statusparameter or by passing-PresentOnlyas parameter)
Below is output on a Dutch system showing filtering during and after query.
Filtering during query using the -InstanceId "USB\*" parameter:
PS C:\bin> Get-PnpDevice -InstanceId "USB\*" | Select-Object * -ExcludeProperty ConfigManagerErrorCode,ProblemDescription,Caption,Description,FriendlyName,InstallDate,Availability,ConfigManagerUserConfig,DeviceID,ErrorCleared,ErrorDescription,LastErrorCode,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,SystemCreationClassName,SystemName,StatusInfo,CompatibleID,HardwareID,Manufacturer,PNPClass,PSComputerName,CimClass,CimInstanceProperties,CimSystemProperties | Format-Table Class InstanceId Problem Name Status CreationClassName ClassGuid Present Service ----- ---------- ------- ---- ------ ----------------- --------- ------- ------- SCSIAdapter USB\VID_174C&PID_55AA\MSFT300420050639A8 CM_PROB_PHANTOM Via USB aangesloten SCSI (UAS)-apparaat voor massaopslag Unknown Win32_PnPEntity {4d36e97b-e325-11ce-bfc1-08002be10318} False UASPStor USB USB\VID_0557&PID_2221\5&273B0B4C&0&8 CM_PROB_NONE Samengesteld USB-apparaat OK Win32_PnPEntity {36fc9e60-c465-11cf-8056-444553540000} True usbccgp HIDClass USB\VID_0557&PID_2221&MI_01\6&6592382&0&0001 CM_PROB_NONE USB-invoerapparaat OK Win32_PnPEntity {745a17a0-74d3-11d0-b6fe-00a0c90f57da} True HidUsb HIDClass USB\VID_0557&PID_2221&MI_00\6&6592382&0&0000 CM_PROB_NONE USB-invoerapparaat OK Win32_PnPEntity {745a17a0-74d3-11d0-b6fe-00a0c90f57da} True HidUsb SCSIAdapter USB\VID_174C&PID_55AA\MSFT30042005060DC6 CM_PROB_PHANTOM Via USB aangesloten SCSI (UAS)-apparaat voor massaopslag Unknown Win32_PnPEntity {4d36e97b-e325-11ce-bfc1-08002be10318} False UASPStor USB USB\ROOT_HUB30\4&2CAC3E6&0&0 CM_PROB_NONE USB-hoofdhub (USB 3.0) OK Win32_PnPEntity {36fc9e60-c465-11cf-8056-444553540000} True USBHUB3
Filtering after query using the Where-Object InstanceId -Like "USB\*" command:
PS C:\bin> Get-PnpDevice | Where-Object InstanceId -Like "USB\*" | Select-Object * -ExcludeProperty ConfigManagerErrorCode,ProblemDescription,Caption,Description,FriendlyName,InstallDate,Availability,ConfigManagerUserConfig,DeviceID,ErrorCleared,ErrorDescription,LastErrorCode,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,SystemCreationClassName,SystemName,StatusInfo,CompatibleID,HardwareID,Manufacturer,PNPClass,PSComputerName,CimClass,CimInstanceProperties,CimSystemProperties | Format-Table Class InstanceId Problem Name Status CreationClassName ClassGuid Present Service ----- ---------- ------- ---- ------ ----------------- --------- ------- ------- SCSIAdapter USB\VID_174C&PID_55AA\MSFT300420050639A8 CM_PROB_PHANTOM Via USB aangesloten SCSI (UAS)-apparaat voor massaopslag Unknown Win32_PnPEntity {4d36e97b-e325-11ce-bfc1-08002be10318} False UASPStor USB USB\VID_0557&PID_2221\5&273B0B4C&0&8 CM_PROB_NONE Samengesteld USB-apparaat OK Win32_PnPEntity {36fc9e60-c465-11cf-8056-444553540000} True usbccgp HIDClass USB\VID_0557&PID_2221&MI_01\6&6592382&0&0001 CM_PROB_NONE USB-invoerapparaat OK Win32_PnPEntity {745a17a0-74d3-11d0-b6fe-00a0c90f57da} True HidUsb HIDClass USB\VID_0557&PID_2221&MI_00\6&6592382&0&0000 CM_PROB_NONE USB-invoerapparaat OK Win32_PnPEntity {745a17a0-74d3-11d0-b6fe-00a0c90f57da} True HidUsb SCSIAdapter USB\VID_174C&PID_55AA\MSFT30042005060DC6 CM_PROB_PHANTOM Via USB aangesloten SCSI (UAS)-apparaat voor massaopslag Unknown Win32_PnPEntity {4d36e97b-e325-11ce-bfc1-08002be10318} False UASPStor USB USB\ROOT_HUB30\4&2CAC3E6&0&0 CM_PROB_NONE USB-hoofdhub (USB 3.0) OK Win32_PnPEntity {36fc9e60-c465-11cf-8056-444553540000} True USBHUB3
Almost equivalents to the last one are (skipping disconnected devices)
PS C:\bin> Get-WmiObject Win32_PnPEntity | Where-Object DeviceID -Like "USB\*" | Select-Object * -ExcludeProperty PSComputerName,Availability,Caption,CompatibleID,ConfigManagerErrorCode,ConfigManagerUserConfig,Description,ErrorCleared,ErrorDescription,FriendlyName,HardwareID,InstallDate,LastErrorCode,Manufacturer,PNPClass,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,StatusInfo,SystemCreationClassName,SystemCreationClassName,SystemName,__CLASS,__DERIVATION,__DYNASTY,__GENUS,__NAMESPACE,__PATH,__PROPERTY_COUNT,__RELPATH,__SERVER,__SUPERCLASS,Scope,Path,Options,ClassPath,Properties,SystemProperties,Qualifiers,Site,Container | Format-Table ClassGuid CreationClassName DeviceID Name Present Service Status --------- ----------------- -------- ---- ------- ------- ------ {36fc9e60-c465-11cf-8056-444553540000} Win32_PnPEntity USB\VID_0557&PID_2221\5&273B0B4C&0&8 Samengesteld USB-apparaat True usbccgp OK {745a17a0-74d3-11d0-b6fe-00a0c90f57da} Win32_PnPEntity USB\VID_0557&PID_2221&MI_01\6&6592382&0&0001 USB-invoerapparaat True HidUsb OK {745a17a0-74d3-11d0-b6fe-00a0c90f57da} Win32_PnPEntity USB\VID_0557&PID_2221&MI_00\6&6592382&0&0000 USB-invoerapparaat True HidUsb OK {36fc9e60-c465-11cf-8056-444553540000} Win32_PnPEntity USB\ROOT_HUB30\4&2CAC3E6&0&0 USB-hoofdhub (USB 3.0) True USBHUB3 OK
and
PS C:\bin> Get-CimInstance -ClassName Win32_PnPEntity | Where-Object InstanceId -Like "USB\*" | Select-Object * -ExcludeProperty Availability,Caption,CimClass,CimInstanceProperties,CimSystemProperties,CompatibleID,ConfigManagerErrorCode,ConfigManagerUserConfig,Description,DeviceID,ErrorCleared,ErrorDescription,FriendlyName,HardwareID,InstallDate,LastErrorCode,Manufacturer,PNPClass,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,ProblemDescription,PSComputerName,StatusInfo,SystemCreationClassName,SystemCreationClassName,SystemName | Format-Table Class InstanceId Problem Name Status CreationClassName ClassGuid Present Service ----- ---------- ------- ---- ------ ----------------- --------- ------- ------- USB USB\VID_0557&PID_2221\5&273B0B4C&0&8 CM_PROB_NONE Samengesteld USB-apparaat OK Win32_PnPEntity {36fc9e60-c465-11cf-8056-444553540000} True usbccgp HIDClass USB\VID_0557&PID_2221&MI_01\6&6592382&0&0001 CM_PROB_NONE USB-invoerapparaat OK Win32_PnPEntity {745a17a0-74d3-11d0-b6fe-00a0c90f57da} True HidUsb HIDClass USB\VID_0557&PID_2221&MI_00\6&6592382&0&0000 CM_PROB_NONE USB-invoerapparaat OK Win32_PnPEntity {745a17a0-74d3-11d0-b6fe-00a0c90f57da} True HidUsb USB USB\ROOT_HUB30\4&2CAC3E6&0&0 CM_PROB_NONE USB-hoofdhub (USB 3.0) OK Win32_PnPEntity {36fc9e60-c465-11cf-8056-444553540000} True USBHUB3
You see that Get-WmiObject:
- needs many more columns excluded for display than
Get-CimInstanceandGet-PnpDevice - uses
DeviceIDwhereas bothGet-CimInstanceandGet-PnpDeviceuseInstanceId
Note that for all three, Get-Member do not show all properties to exclude. That process is a trial and error.
The DiskDrive -Class
This is a query to get all your disks (now on an English system):
PS C:\bin> Get-PnpDevice -Class 'DiskDrive' | Select-Object * -ExcludeProperty ConfigManagerErrorCode,ProblemDescription,Caption,Description,FriendlyName,InstallDate,Availability,ConfigManagerUserConfig,DeviceID,ErrorCleared,ErrorDescription,LastErrorCode,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,SystemCreationClassName,SystemName,StatusInfo,CompatibleID,HardwareID,Manufacturer,PNPClass,PSComputerName,CimClass,CimInstanceProperties,CimSystemProperties | Format-Table Class InstanceId Problem Name Status CreationClassName ClassGuid Present Service ----- ---------- ------- ---- ------ ----------------- --------- ------- ------- DiskDrive SCSI\DISK&VEN_SK&PROD_HYNIX\4&245A28B&0&000100 CM_PROB_NONE SK hynix SC401 SATA 256GB OK Win32_PnPEntity {4d36e967-e325-11ce-bfc1-08002be10318} True disk DiskDrive SCSI\DISK&VEN_ST500LM0&PROD_34-2GH17A\4&245A28B&0&000000 CM_PROB_NONE ST500LM034-2GH17A OK Win32_PnPEntity {4d36e967-e325-11ce-bfc1-08002be10318} True disk
Note the above filters on -Class without wildcards, and uses single-quotes.
Using single-quotes (') makes it a lot easier to embed a PowerShell command in a batch file enclosing it in double-quotes (") like in this PowerShell-Get-PnpDevice-DiskDrive.bat:
:: https://wiert.wordpress.com/?p=124116 PowerShell "Get-PnpDevice -Class 'DiskDrive' | Select-Object * -ExcludeProperty ConfigManagerErrorCode,ProblemDescription,Caption,Description,FriendlyName,InstallDate,Availability,ConfigManagerUserConfig,DeviceID,ErrorCleared,ErrorDescription,LastErrorCode,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,SystemCreationClassName,SystemName,StatusInfo,CompatibleID,HardwareID,Manufacturer,PNPClass,PSComputerName,CimClass,CimInstanceProperties,CimSystemProperties | Format-Table"
Querying other hardware than Win32_PnpEntity
When you need to query other types of hardware, then read [Wayback/Archive] Should I use CIM or WMI with Windows PowerShell? – Scripting Blog.
It basically comes down to using Get-CimInstance as it is both better integrated into PowerShell which you can for instance see in Date/Time handling and the need to exclude far less columns from overviews.
There are a truckload of WMI (Windows Management Instrumentation) and CIM (Common Information Model) classes which you can retrieve using these statements:
Get-WmiObject -List | Select-Object NameGet-CimClass | Select-Object CimClassName
The last can also filter the classname during the query:
Get-CimClass -ClassName *Disk* | Select-Object CimClassName | Sort-Object CimClassName
The first can only filter after the query:
Get-WmiObject -List | Where-Object Name -Like "*Disk*" | Select-Object Name | Sort-Object Name
The above Scripting Blog post list a few more reasons why you should prefer CIM over WMI.
Microsoft Learn links
- [Wayback/Archive]
Get-CimClass(CimCmdlets) – PowerShell | Microsoft Learn - [Wayback/Archive]
Get-CimInstance(CimCmdlets) – PowerShell | Microsoft Learn - [Wayback/Archive]
Get-Member(Microsoft.PowerShell.Utility) – PowerShell | Microsoft Learn - [Wayback/Archive]
Get-PnpDevice(PnpDevice) | Microsoft Learn - [Wayback/Archive]
Get-WmiObject(Microsoft.PowerShell.Management) – PowerShell | Microsoft Learn - [Wayback/Archive]
Where-Object(Microsoft.PowerShell.Core) – PowerShell | Microsoft Learn - [Wayback/Archive]
Win32_PnPEntityclass – Win32 apps | Microsoft Learn
–jeroen







Leave a comment