Get-WmiObject filter question

Welcome Forums General PowerShell Q&A Get-WmiObject filter question

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

1 year ago.

  • Author
  • #101016

    Topics: 5
    Replies: 2
    Points: 1
    Rank: Member

    I need to run a Get-WmiObject command that filters for 2 criteria.

    I need to combine the following 2 filters...but for the life of me, I can't figure out how to use the -And operator to get it to work.

    Should I be using a -Filter or a Where statement?

    Get-WmiObject -Namespace root\cimv2\sms -class SMS_InstalledSoftware -Filter "ARPDisplayName LIKE 'Box for Office'"
    Get-WmiObject -Namespace root\cimv2\sms -class SMS_InstalledSoftware -Filter "ProductVersion LIKE '4.3.1217.0'"

    Thanks very much.

  • #101019

    Topics: 4
    Replies: 76
    Points: 328
    Helping Hand
    Rank: Contributor

    The -Filter paremeter for Get-WMIObject is looking for WQL syntax

    Get-Help Get-WmiObject -Parameter Filter
        Specifies a Where clause to use as a filter. Uses the syntax of the WMI Query Language (WQL).
        Important: Do not include the Where keyword in the value of the parameter. For example, the following commands return only the logical disks that have a DeviceID of 'c:' 
        and services that have the name 'WinRM' without using the Where keyword.
        `Get-WmiObject Win32_LogicalDisk -filter "DeviceID = 'c:' "`
        `Get-WmiObject win32_service -filter "name='WinRM'"`
        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?       False
        Accept wildcard characters?  false

    Unless you enjoy WQL a Where-Object would be easier IMO.

  • #101020

    Topics: 5
    Replies: 2
    Points: 1
    Rank: Member

    Thanks. It looks like the following is going to work for me.

    Get-WmiObject -Namespace root\cimv2\sms -class SMS_InstalledSoftware | Where {$_.ARPDisplayname -eq 'Box for Office' -and $_.ProductVersion -eq '4.3.1217.0'}
  • #101026

    Topics: 8
    Replies: 1191
    Points: 644
    Helping Hand
    Rank: Major Contributor

    You should always filter as far left as possible, so you should leverage the WMI filter. WQL does support LIKE, however, in your posted examples I didn't see a wildcard character (but it could be the site). This article discusses WQL Operators:

    PS C:\Users\Rob> Get-WMIObject -Class Win32_Service -filter "Name like 'box%'"
    ExitCode  : 0
    Name      : BoxSyncUpdateService
    ProcessId : 0
    StartMode : Manual
    State     : Stopped
    Status    : OK
    PS C:\Users\Rob> Get-CimInstance -ClassName Win32_Service -Filter "Name like 'box%'"
    ProcessId Name                 StartMode State   Status ExitCode
    --------- ----                 --------- -----   ------ --------
    0         BoxSyncUpdateService Manual    Stopped OK     0       
  • #101043

    Topics: 22
    Replies: 616
    Points: 1,271
    Helping Hand
    Rank: Community Hero

    A -filter (with this command) is the same as the where clause in sql, and a -query is the same as sql. Just use 'and'. You can use '=' instead of 'like', since you aren't using the '%' wildcard. A -filter is faster than piping to where-object, if you're dealing with a lot of instances. '=' is faster than 'like' as well.

    Get-WmiObject -Namespace root\cimv2\sms -class SMS_InstalledSoftware -Filter "
      ARPDisplayName = 'Box for Office' and ProductVersion = '4.3.1217.0'"

    A good example for a filter (have to double up the backslashes), since there's a ton of directory instances (95,000 on my computer):

    Get-WmiObject win32_directory -filter "name = 'c:\\' or name = 'c:\\users'"

The topic ‘Get-WmiObject filter question’ is closed to new replies.