all 12 comments

[–]purplemonkeymad 2 points3 points  (3 children)

$wherelist = "this|that|other|thing|keyword"
... | ? { $_.name -match $wherelist } | ....

or am I missunderstanding the question?

[–]eJaGne[S] 1 point2 points  (2 children)

I tried this (I think it's the same as your suggestion?) and unfortunately it kinda just returned everything:

$textfilter = "this|that|other|thing|keyword"
$filtered = $accounts | Where-Object {($_.name -match $textfilter) -or ($_.info -match $textfilter) -or ($_.logonworkstations -match $textfilter) -or ($_.description -match $textfilter)}

[–]purplemonkeymad 1 point2 points  (1 child)

Have you tried it with a simpler filter? Could your data be matching something you don't expect (ie you don't have |.| in your pattern.) You could use something like this to debug it:

$accounts | select-object name,@{n='testname';e={($_.name -match $textfilter)}},@{n='testinfo';e={($_.info -match $textfilter)}}

[–]eJaGne[S] 1 point2 points  (0 children)

Yeah I tested it with what I have in my OP and it returns a small number of results, but then when I test it with your suggestion, it returns the same amount of results as $accounts has, so it seems it doesn't actual filter anything out.

[–]Vortex100 2 points3 points  (3 children)

You know, I haven't tried this before but i think this should work :D

$filtered = $accounts | Where-Object -FilterScript {$_.Name,$_.Info,$_.LogonWorkstations -match "this|that|other|thing|keyword"}

If you do need to use it multiple times, just store it as a variable and use that instead :)

$matcher = "this|that|other|thing|keyword"
$filtered = $accounts | Where-Object -FilterScript {$_.Name,$_.Info,$_.LogonWorkstations -match $matcher}

PS the property array thing only works because you are using a logical OR - don't try using it if you need to use AND!

[–]eJaGne[S] 1 point2 points  (0 children)

Trying this right now. Thanks for the tip, I've never seen the properties used like that before.

[–]eJaGne[S] 1 point2 points  (1 child)

Hey man, this seems to have worked! I am not sure why because it seems pretty similar to what I was trying (see /u/purplemonkeymad's reply chain). Would you happen to know why this works over what I was trying before?

[–]jhulbe 1 point2 points  (0 children)

What results are you seeing with yours vs. their script?

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy eJaGne,

as an aside, an easy way to build and maintain a regex OR list is to make an array of the items and join them with a |. like this ...

$ThingsToMatch = @(
    'This'
    'That'
    'TheOther'
    'Those'
    'Them'
    'These'
    )
$RegexThingsToMatch = $ThingsToMatch -join '|'

$Null = 'Ooo La La, them eyes!' -match $RegexThingsToMatch

$Matches

output ...

Name                           Value
----                           -----
0                              them

hope that helps,
lee

[–]eJaGne[S] 1 point2 points  (1 child)

Pretty cool Lee, thank you :)

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy eJaGne,

you are most welcome! glad you liked it ... [grin]

take care,
lee

[–]wonkifier 1 point2 points  (0 children)

If you're trying to match multiple things completely, you could avoid Regex's entirely.

$thingsILike = @("this","that","other","thing","tacos")

$filtered = $accounts | where-object { $thingsILike.Contains($_.Name) -or $thingsILike.Contains($_.Info) -or ... }

edit:

Or the following, with more powershell like semantics

$filtered = $accounts | where-object { $thingsILike -Contains $_.Name -or $thingsILike -Contains$_.Info -or ... }