none
Using the match feature throughan array RRS feed

  • Question

  • I've been trying to write a script and in a section of it, I need to compare names of users to a explicit list. So Powershell is presented with the names of all users on a specific system and I need to find any users that start with a certain string. All accounts that need to be counted start with "xxx" or "yyy" but have varying length.

    Now this bit is fine and I could use something like the below line to count these.

    Where-Object {$_.name -Match "xxx"} | Select Name | Measure

    But I don't want to have multiple lines doing the same thing so comparing them to items in an array would be great. For this, I have the below lines.

    $valuesToMatch = @(
        "xxx"
        "yyy"
        )
    
    Where-Object {$valuesToMatch -Match $_.name}

    But when I am using the method above (I have, of course, missed out various lines for sake of simplicity) it only counts it if it is an exact match. A.K.A. if the list of users was xxx, xxxbob this would only match with xxx and not xxxbob.

    With the first line it matched to include but when checking from the array it has to be exact, what is the cause / workaround of this?

    Friday, November 15, 2019 9:48 AM

Answers

  • Well you can have an array on the left side of the match operator, but the search pattern goes on the right side.

    You can join the patterns on the right with a '|' like this, and it will match either one:


    $valuesToMatch = 'xxx','yyy' -join '|'
    Where name -Match $valuesToMatch

    Friday, November 15, 2019 12:43 PM

All replies

  • "match" does not work with arrays.  You also have to use of match wrong.  Read the docs for instructions.

    help about_Regular_Expressions

    $_.Name -match 'xxx|yyy'


    \_(ツ)_/

    Friday, November 15, 2019 9:55 AM
    Moderator
  • Well you can have an array on the left side of the match operator, but the search pattern goes on the right side.

    You can join the patterns on the right with a '|' like this, and it will match either one:


    $valuesToMatch = 'xxx','yyy' -join '|'
    Where name -Match $valuesToMatch

    Friday, November 15, 2019 12:43 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 18, 2019 6:54 AM
    Moderator
  • Select-string can take an array of patterns, but it outputs a matchinfo object, not a string:


    echo joe john james | select-string jo,ja | % line

    • Edited by JS2010 Monday, November 18, 2019 5:16 PM
    Monday, November 18, 2019 2:37 PM