3

I have this situation in my script:

$Excludes = "Commvault","Veeam"

$testuser = 'DOMAIN\vCommvaultConnect'
$Excludes | ForEach-Object {
    If ($testuser.Contains($_)) {
    Write-Host "Found"
    }
}

Is this the most efficient way to test for this or is there a faster way to match a user to each of those excluded words?

0

1 Answer 1

1

How about this, using Regex search groups

$Excludes = "Commvault","Veeam"

$SearchRegex_Excludes = ($Excludes | % { "(" + ($_) + ")" }) -join "|"
# sample regex pattern result - (Commvault)|(Veeam)

$testuser = "DOMAIN\vCommvaultConnect"

if ( $testuser -match $SearchRegex_Excludes ) { "Found" } else { "Not Found " }
Sign up to request clarification or add additional context in comments.

4 Comments

Is this more efficient or faster than the original? If yes - why?
It's both, Just use measure-command on both. Thanks Karthick, this is what I was trying to remmeber how to do. The excludes variable is only used for this, so I can define it as a Regex group anyway: $Excludes = "Commvault|Veeam"
I did measure. OK. If that difference is what matters for you. ;-)
Can you explain why this is more efficient?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.