4

I'm trying to filter results of a wmic list with where-object but unfortunately this does not seems to work.

wmic diskdrive list brief  | Where-Object -Property "Caption" -Contains "Samsung"

The filtered result is empty, the unfiltered result is as such:

Caption                       DeviceID            Model                Partitions  Size

HGST HUH721212ALE600          \\.\PHYSICALDRIVE1  HGST HUH721212ALE600 1           12000132426240
OCZ-VERTEX3                   \\.\PHYSICALDRIVE3  OCZ-VERTEX3          1           240054796800
Samsung SSD 970 EVO Plus 1TB  \\.\PHYSICALDRIVE5  Samsung SSD 970 EVO Plus 1TB  4           1000202273280
ST16000NM001G-2KK103          \\.\PHYSICALDRIVE4  ST16000NM001G-2KK103 1           16000900392960
ST6000NM0024-1HT17Z           \\.\PHYSICALDRIVE0  ST6000NM0024-1HT17Z  1           6001172513280
Samsung SSD 840 PRO Series    \\.\PHYSICALDRIVE2  Samsung SSD 840 PRO Series    1           512105932800

How to filter properly ?

In the end, I want to get the DeviceID, eg., PHYSICALDRIVE5

  • 2
    is there a reason NOT to use the built in PoSh cmdlet for this? if not, then try this ... >>> Get-Disk | Where-Object Manufacturer -Match 'crucial' <<< for me, that gives the info for my solitary SSD. [grin] – Lee_Dailey Aug 23 at 18:50
8

WMIC is not a PowerShell cmdlet, and doesn't contain the properties Where-Object is looking for.

You can verify this by trying to pipe your command to Get-Member.

Using Select-String as mentioned in the other answer is a great way to filter any output in PowerShell and is worth remembering.

You could also get the information you are getting from WMIC from a PowerShell cmdlet, and then all the PowerShell filtering and property commands come alive.

Get-CimInstance Win32_DiskDrive | Where-Object {$_.Caption -like "*samsung*" }

Running this on my system with AMD produces:

PS C:\Users\ishmael> Get-CimInstance Win32_DiskDrive | Where-Object {$_.Caption -like "*AMD*" }

DeviceID           Caption                                   Partitions Size          Model
--------           -------                                   ---------- ----          -----
\\.\PHYSICALDRIVE2 AMD-RAID Samsung SSD 970 SCSI Disk Device 3          1000202273280 AMD-RAID Samsung SSD 970 SCSI Disk Device
\\.\PHYSICALDRIVE1 AMD-RAID Array 2  SCSI Disk Device        1          999091860480  AMD-RAID Array 2  SCSI Disk Device

Command Breakdown

Get-CimInstance

CIM Cmdlets were added as a replacement to the WMI Cmdlets. CIM stands for Common Information Model and is used in this instance to access a standardized set of information about Windows. DMTF RedGate MS CimCmdlets

Win32_DiskDrive

One of the CIM classes that is available to query. There are many CIM classes. Try Get-CimClass Win32* to get an idea of the types of CIM classes available that are prefix with Win32.

|

Pipe. Sends the output on the left to the command on the right.

Where-Object

Where-Object can be used to filter PowerShell output that has been piped to it. To see what kind of objects that can be filtered, pipe your command to Get-Member first, which will show you a list of the possible objects.

{$_.Caption -like "samsung"}

  • $_ is the PowerShell syntax for self. Is referring to the piped input.
  • .Caption the member object of $_ we would like to query
  • -like PowerShell comparison operator that allows wildcard patterns PS Operators
  • "*samsung*" our search string, the wildcard asterisks are leading and following our string to ensure we match samsung anywhere in the output. Leaving out the leading wildcard will show results that only start with samsung.
| improve this answer | |
5

I'm trying to filter results of a wmic list with where-object but this does not work.

I'm using:

wmic diskdrive list brief  | Where-Object -Property "Caption" -Contains "Samsung"

To find the lines containing the string "Samsung" use the following command:

wmic diskdrive list brief  | select-string -pattern "Samsung*"

With your example it should return:

Samsung SSD 970 EVO Plus 1TB  \\.\PHYSICALDRIVE5  Samsung SSD 970 EVO Plus 1TB  4           1000202273280
Samsung SSD 840 PRO Series    \\.\PHYSICALDRIVE2  Samsung SSD 840 PRO Series    1           512105932800
| improve this answer | |
1

this does not directly Answer your Question. [grin] however, it gives a more "powershell-ish" version of the WMIC.exe call you used.

what it does ...

  • sets the target string to filter with
  • gets the disk info for all disks
  • filters out the unwanted disks
  • builds a new object with just the wanted properties
    the WMIC "DeviceID" item seems to map to the ".DiskNumber" property.
  • trims away the unwanted "bunch of leading whitespace" in my serial number property
  • sends that to a $Var
  • displays that on screen

here's the code ...

$Maker = 'crucial'

$TargetDiskInfo = Get-Disk |
    Where-Object -Property FriendlyName -Match $Maker |
    Select-Object -Property DiskNumber, Manufacturer, Model,
        # the SN on my ssd has a BUNCH of leading whitespace
        #    the ".Trim()" gets rid of that
        @{n = 'SerialNumber'; e = {$_.SerialNumber.Trim()}}

$TargetDiskInfo

the output ...

DiskNumber Manufacturer Model            SerialNumber
---------- ------------ -----            ------------
         0 Crucial_     CT1024MX200S     153910A893E4
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.