-2

In the below code, if I add a where-object, -lt & -gt are giving opposite of expected results.

I'm sure the reason is I'm stupid, but in what specific way am I messing up?

This part gives the expected results, where in my case, the single drive has %Free of 39.8

Get-WmiObject -Namespace root\cimv2 -Class win32_logicaldisk | where-object -Property drivetype -eq 3 | 
format-table deviceid,
@{n='GB Capacity';e={$_.size/1gb}},
@{n='GB Free';e={$_.freespace/1gb}},
@{n='%Free';e={($_.freespace/$_.size)*100}}

But adding this

| where {$_.'%Free' -gt 10}

Results in no output. In fact

| where {$_.'%Free' -gt 0}

Produces no results. Instead I have to use

| where {$_.'%Free' -lt 0}

Powershell thinks %Free is a negative number, I guess?

bbb0777
  • 165
  • 14

1 Answers1

4

The problem is that you are piping Format-Table to anything. You should never use that except to output to the screen. Using Format-Table outputs everything as a format object, not whatever was piped into it. Instead use Select-Object to get what you need.

Get-WmiObject -Namespace root\cimv2 -Class win32_logicaldisk | where-object -Property drivetype -eq 3 | 
Select-Object deviceid,
@{n='GB Capacity';e={$_.size/1gb}},
@{n='GB Free';e={$_.freespace/1gb}},
@{n='%Free';e={($_.freespace/$_.size)*100}}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56