JP Legris
JP Legris

Reputation: 21

PowerShell Get-WmiObject WIN32_VOLUME WHERE clause

Here is My Issue. What I am trying to achieve is to display only the information where the drive type is 3(which are HardDrives) but that actually has a driveletter :

GET-WMIOBJECT WIN32_VOLUME | where { $_.drivetype -eq '3'}| select-object freespace, capacity, drivetype, driveletter

Currently it is displaying :

                freespace                      capacity                     drivetype driveletter
                ---------                      --------                     --------- -----------
                273465344                     314568704                             3
              58966519808                  128522907648                             3 C:

So the first line I don't want. I've tried adding:

where { $_.drivetype -eq '3' and $_.driveletter -ne null } and where { $_.drivetype -eq '3'} and {$_.driveletter -ne null }

Upvotes: 1

Views: 7444

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174475

To filter out volumes with no drive letter, treat the DriveLetter property as a boolean (both $null and empty strings are interpreted as $false) inside Where-Object.

Make sure your use -and instead of and (notice the dash):

Get-WmiObject Win32_Volume |Where { $_.drivetype -eq '3' -and $_.driveletter} |Select-Object freespace,capacity,drivetype,driveletter |Format-Table -HideTableHeaders

(I totally misread your question at first, here is the original answer):

The output is implicitly piped through Format-Table which generates the table headers you see in the output.

You can call Format-Table yourself with the -HideTableHeaders switch parameter to void them from the output:

Get-WmiObject Win32_Volume |Where { $_.drivetype -eq '3'} |Select-Object freespace,capacity,drivetype,driveletter |Format-Table -HideTableHeaders

Upvotes: 3

Related Questions