Wild cards in an array

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 1 year, 3 months ago.

  • Author
    Posts
  • #30313
    Profile photo of Bryan Sapen
    Bryan Sapen
    Participant

    OK, real newbie here, I would like to compare an array that contains wild cards with the results from a cmdlet.

    For example,

    $ListOfNames = @("_a*", "_b*", "_c*", "_d*")
    get-something | where {($_.somename -notin $ListOfNames)}
    

    I know this is not supported but I'm looking for anyway to accomplish this.

    Thanks...

  • #30322
    Profile photo of David Johnson
    David Johnson
    Participant

    Nice question. Here's how I would solve it:

    $ListOfNames = @("_a*", "_b*", "_c*", "_d*")
    
    get-something | Where { 
        @(Foreach ($Check in $ListofNames) {
        $_.SomeName -notlike $Check
        }) -notcontains $False
    
    }
    

    Change the 'Where' to a 'ForEach' to get an indication of what's happening.

  • #30328
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    I would use Select-String and regular expressions as search patterns

    $ListOfNames = @("^a","^b","^n")
    dir | Where-Object {$_.Name | Select-String -Pattern $ListOfNames}
    

    Results similar to:

    Mode                LastWriteTime     Length Name                                                                                                                                                                               
    ----                -------------     ------ ----                                                                                                                                                                               
    -a---         5/21/2013   3:13 PM      28163 Available Storage.xml                                                                                                                                
    -a---         4/23/2013   4:59 PM        167 Navi.xml                                                                                                                                                               
    -a---         5/21/2013   4:19 PM        360 Nav.xml                               
    

    or to reverse it:

    $ListOfNames = @("^a","^b","^n")
    dir | Where-Object {-NOT ($_.Name | Select-String -Pattern $ListOfNames)}
    

    Results similar to:

    Mode                LastWriteTime     Length Name                                                                                                                                                                               
    -a---          5/3/2015   5:52 PM     204800 0763dcb4.db4                                                                                                                                                                       
    -a---         4/28/2015   4:17 PM     204960 0893e0b4.db4                                                                                                                                                                       
    -a---          5/1/2013  10:09 AM      25211 LUNsFun.xml                                                                                                                                               
    -a---          5/1/2013  12:07 PM      25211 LUNs.xml                                                                                                                                               
    -a---         10/1/2015  11:47 AM     795790 PandaGPA.log                                        
    

You must be logged in to reply to this topic.