Get-WmiObject filter question

This topic contains 4 replies, has 4 voices, and was last updated by  js 1 month ago.

  • Author
    Posts
  • #101016

    JohnDav
    Participant

    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

    L-Bo
    Participant

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

    Get-Help Get-WmiObject -Parameter Filter
    
    -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

    JohnDav
    Participant

    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

    Rob Simmers
    Participant

    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: https://msdn.microsoft.com/en-us/library/aa394605(v=vs.85).aspx

    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

    js
    Participant

    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'"
    

You must be logged in to reply to this topic.