Query not returning expected results

Running PS v2, and have a very simple query that isn’t returinng what I expect.

(Get-WmiObject -class Win32_OperatingSystem).Caption
If ((Get-WmiObject -class Win32_OperatingSystem).Caption -eq 'Microsoft Windows 7 Enterprise') {
    Write-Host "Equals"
    } Else {
    write-host "not equals"

It returns this -

Microsoft Windows 7 Enterprise
not equals

The WMI Caption info is as expected. however when passed throguh the compare in my IF, its not returning what I expect(returns ‘not equal’ rather than ‘equal’).

No doubt something simple, but I just can’t see it.


The WMI query returns a string with a trailing space.

'Microsoft Windows 7 Enterprise ’

You can use that string to compare, or do a trim on your WMI query result

(Get-WMIObject -class Win32_OperatingSystem).Caption.Trim()

Perfect. Thanks