Query not returning expected results

Welcome Forums General PowerShell Q&A Query not returning expected results

This topic contains 2 replies, has 2 voices, and was last updated by

5 years ago.

  • Author
  • #11595

    Points: 1
    Rank: Member

    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.


  • #11599

    Points: 0
    Rank: Member

    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()

  • #11603

    Points: 1
    Rank: Member

    Perfect. Thanks

The topic ‘Query not returning expected results’ is closed to new replies.