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 cmldet and filtering left with its DriveLetter parameter. Using either an upper or lowercase ‘C’ returns the same results:

Get-Volume -DriveLetter 'C'
Get-Volume -DriveLetter 'c'

filtering-issue1.jpg

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:

Get-Volume | where DriveLetter -eq 'C'
Get-Volume | where DriveLetter -eq 'c'

filtering-issue2.jpg

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.

help Get-Volume -Parameter DriveLetter
Get-Volume | Get-Member

filtering-issue3.jpg

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:

[char]$charTest = 'C'
$charTest
$charTest -eq 'C'
$charTest -eq 'c'
$charTest -eq 'D'
$charTest | gm

filtering-issue4.jpg

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:

Get-Volume | where {$_.DriveLetter -eq 'C'}
Get-Volume | where {$_.DriveLetter -eq 'c'}

filtering-issue5.jpg

Be sure to read my next blog where I followed up on this issue and discovered it doesn’t occur on two other Windows 8 machines that I tested it on. This is a very strange issue. Also be sure to read Peter Kriegel’s comment on this blog article (I forgot all about implicit conversions of data types).

µ