PowerShell - Using notcontains, notlike, and notmatch for multiple items. RRS feed

  • Question

  • Hello All

    Ok, this is a little bit of a logic puzzle, but with a serious use if it can be sorted out.  I'm curious to know if there is a smooth way (or if it's even possible) to use NOTLIKE or NOTMATCH for multiple comparisons in a Where-Object call? 



    • Edited by sgrinker Monday, May 9, 2011 7:45 PM
    Monday, May 9, 2011 7:02 PM


All replies

  • Hi,

    Please try posting your code again, giving short examples of what output you'd like vs. what you're actually seeing. Your code examples have four blank lines between each line of code, making it very difficult to read.


    Monday, May 9, 2011 7:16 PM
  • Sorry about that...   definitely hard to read!  Apparently the copy and paste of the code examples added lines after I submitted the post.  I'll try to briefly summarize below with few less examples, and edit the post above.

    Example #1

    $StringArray = @("Thing1","Thing2","Thing3","Thing4","Thing5","Thing6","Thing7","Thing8")
    $ContainsArray = @("Thing2","Thing4","Thing6","Thing8")
    $stringarray | where-object {$ContainsArray -contains $_}
    $stringarray | where-object {$ContainsArray -notcontains $_}

    The first where-object will return only the items contained in the ContainsArray, while the second returns the inverse.  Both would be expected behavior.  However, assuming that you did not have exact matches and instead wanted to use wildcards, is there a way to use NOTLIKE or NOTMATCH to basically return every object in a collection that does not exist within a set of wildcards?

    Example #2

    $StringArray = @("Red-Thing1","Blue-Thing2","Orange-Thing3","Red-Thing4","Blue-Thing5","Orange-Thing6","Red-Thing7","Blue-Thing8")

    Is there a way to use NOTLIKE or NOTMATCH with a Where-Object statement similar to the ones above and return all of the objects that do not mactch either "Red*" or "Blue*" ?  From the testing I've done, NOTLIKE and NOTMATCH don't seeem to work well with an array of matches.  I can get either "not red" or "not blue", but not both at the same time.  Other options are to iterate through the collection and match each object against each other.  It would seem like the direct pipeline would be faster if it was possible.  It seems like I might just be missing something, so I wanted to throw it out to the community for input. 

    $MatchArray = @("Red","Blue")
    $LikeArray = @("Red*","Blue*")
    $stringarray | where-object {$_ -match $MatchArray}
    $stringarray | where-object {$_ -notmatch $MatchArray}
    $stringarray | where-object {$_ -like $LikeArray}
    $stringarray | where-object {$_ -notlike $LikeArray}
    $stringarray | where-object {$_ -contains "Orange-Thing3"}
    $stringarray | where-object {$_ -match "Blue"}
    $stringarray | where-object {$_ -like "Red*"}

    Any suggestions? 

    Monday, May 9, 2011 7:43 PM
  • See if this helps:


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by sgrinker Monday, May 9, 2011 8:09 PM
    Monday, May 9, 2011 8:01 PM
  • Awesome, thank you!  Funny enough, I think I read that post back in Feb. when it was posted.  Apparently it didn't stick in my memory very well when I actually needed to refrence it.  Thanks for pointing me in the right direction, much appreciated!
    Monday, May 9, 2011 8:09 PM
  • Yep, building the REGEX on the fly from the array definitely did the trick.  Thanks again!
    Monday, May 9, 2011 9:48 PM
  • You're welcome!
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, May 9, 2011 10:15 PM