Jelphy
Jelphy

Reputation: 1059

Where-Object and string interpretation

Using Where-Object, why does this work..

Get-CimInstance -ClassName Win32_MountPoint | Where-Object {$_.Directory -like 'Win32_Directory (Name = "C:\")'}

But this does not:

Get-CimInstance -ClassName Win32_MountPoint | Where-Object {$_.Directory -eq 'Win32_Directory (Name = "C:\")'}

I'm assuming the -eq operator requires additional quotes, or an issue with the speech marks perhaps.

Thanks

Upvotes: 1

Views: 177

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200563

It doesn't work, because the Directory property contains a nested object, not a string. The equality operator thus compares a Win32_Directory object to a string and correctly finds them not equal.

You need to either convert the Directory property to an actual string, e.g. like this:

... | Where-Object { "$($_.Directory)" -eq 'Win32_Directory (Name = "C:\")' }

or like this:

... | Where-Object { $_.Directory.ToString() -eq 'Win32_Directory (Name = "C:\")' }

or (better) check the relevant property of the nested object, e.g. like this:

... | Where-Object { $_.Directory.Name -eq 'C:\' }

Upvotes: 2

Related Questions