locked
Filtering an Array of Objects RRS feed

  • Question

  • I am having trouble visualizing how to accomplish this. I was able to filter w/ -contains if the name is an exact match, but how can I pass in an array of like filters?

    $softwareFilter = ("Adobe","Microsoft","Java")

    Get-WmiObject -Query "Select * From Win32_Product" | Where-Object {$_.Name -like $softwareList }

    Wednesday, January 25, 2012 5:56 PM

Answers

  • Probably easier with a regular expression:

    $softwareList = 'Adobe|Microsoft|Java'

    Get-WmiObject -Query "Select * From Win32_Product" |
     Where-Object {$_.Name -match $softwareList }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Bigteddy Wednesday, January 25, 2012 6:03 PM
    • Edited by mjolinor Wednesday, January 25, 2012 6:06 PM
    • Marked as answer by RowdyBullGaming Wednesday, January 25, 2012 6:21 PM
    Wednesday, January 25, 2012 6:00 PM

All replies

  • Probably easier with a regular expression:

    $softwareList = 'Adobe|Microsoft|Java'

    Get-WmiObject -Query "Select * From Win32_Product" |
     Where-Object {$_.Name -match $softwareList }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Bigteddy Wednesday, January 25, 2012 6:03 PM
    • Edited by mjolinor Wednesday, January 25, 2012 6:06 PM
    • Marked as answer by RowdyBullGaming Wednesday, January 25, 2012 6:21 PM
    Wednesday, January 25, 2012 6:00 PM
  • Mjolinor as usual is correct :)

    Anyway, if you for  some reason you want to keep your array, for further treatment. You can user this little function which convert an array, into a a string for a regular expression valid for -match

    $softwareFilter = ('Adobe','Microsoft','Java')
    $pattern_match = [string]::join('|', ($softwareFilter  | % {[regex]::escape($_)}))
    	
    Get-WmiObject -Query "Select * From Win32_Product" | Where-Object {$_.Name -match $pattern_match }
    


     

    Wednesday, January 25, 2012 6:20 PM
  • Easiest way is with -contains if you are doing an exact match:

    Get-WmiObject -Query "Select * From Win32_Product" | Where-Object {$softwareFilter -contains $_.Name}

    Fastest way (if you have a large # of programs, or large filter list) is with hashtable:

    $softwareFilter = @{"Adobe" = 1;"Microsoft" = 1;"Java" = 1}
    Get-WmiObject -Query "Select * From Win32_Product" | Where-Object { $softwareFilter[$_.Name] }

    Thanks,
    -Lincoln


    Edit: Of course letting WMI do the filtering is really the fastest :)  Could dynamically create the query string based on the filter list.
    Wednesday, January 25, 2012 7:27 PM
  • This should also work:

    $softwareFilter = @("Adobe","Microsoft","Java")
    $filter = ""
    foreach ($item in $softwareFilter) {
      $folder = [regex]::Escape($item)
       $filter += "Name Like '%$folder%' OR "
    }
    $filter = $Filter.TrimEnd(" OR ")
    Get-WmiObject -Query "Select * From Win32_Product Where $filter"

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Wednesday, January 25, 2012 7:55 PM
  • Awesome thanks everyone looks like I have lots of options the first one was the easiest and the one I am using as not all the names are an exact match.

     

    Thanks Again

    Ray

    Thursday, January 26, 2012 12:19 PM