How to properly filter a wmic diskdrive list
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.
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