PowerShell: Filtering Left Could Produce Different Results Than Filtering with Where-Object
While doing some testing on my Windows 8 machine today, I discovered an issue where Filtering Left
could produce different results than filtering with the Where-Object
cmdlet.
Nothing special here. Just using the Get-Volume
cmdlet and filtering left with its DriveLetter
parameter. Using either an upper or lowercase 'C' returns the same results:
1Get-Volume -DriveLetter 'C'
2Get-Volume -DriveLetter 'c'
Using the Where-Object
cmdlet to do the filtering, while not a best practice when the filtering
can be accomplished earlier in the pipeline, should return the same results or at least you would
think it should. Using an upper case 'C' returns the same results, but using a lower case 'c' does
not return any results:
1Get-Volume | where DriveLetter -eq 'C'
2Get-Volume | where DriveLetter -eq 'c'
Looking at both the datatype for the DriveLetter
parameter (input) and the type of object that the
DriveLetter property (output) produces shows they're both [Char]
. Based on this, I would expect
what I'm inputing for the DriveLetter
parameter and what the cmdlet is producing for its output to
the pipeline for the DriveLetter property which is used for input by the Where-Object
cmdlet to
work consistently.
1help Get-Volume -Parameter DriveLetter
2Get-Volume | Get-Member
I'm not a developer and a quick search on the Internet didn't clear up whether the [Char]
data
type in PowerShell is case sensitive or not. So it was time for a quick test to check:
1[char]$charTest = 'C'
2$charTest
3$charTest -eq 'C'
4$charTest -eq 'c'
5$charTest -eq 'D'
6$charTest | gm
I defined a new variable named $charTest that is a [Char]
datatype. I verified what it contained,
tested it against upper and lower case both of which returned True, tested it against another value
which returned False, and finally verified that it was an object type of [Char]
to to make sure it
was the right data type. Based on this test, it doesn't appear that a [Char]
datatype in
PowerShell is case sensitive.
Update:
This appears to be an issue with the new PowerShell version 3 syntax for the Where-Object
cmdlet
which I was using in the previous examples. Using the older PowerShell version 2 syntax for the
Where-Object
cmdlet returns the correct results:
1Get-Volume | where {$_.DriveLetter -eq 'C'}
2Get-Volume | where {$_.DriveLetter -eq 'c'}
µ