Query not returning expected results

This topic contains 2 replies, has 2 voices, and was last updated by  Gerald 4 years, 7 months ago.

  • Author
  • #11595


    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

    Kitt Holland

    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


    Perfect. Thanks

You must be logged in to reply to this topic.