Pierre-Olivier
Pierre-Olivier

Reputation: 13

Powershell Array output somes values with multiple colors

I have two arrays which contain email addresses:

I want to output the value of $from to the user but I want to color the values that are also present in the variable $newuser.

So the output will show all email addresses in $from in white color but show in green the ones that are present in $newuser.

I don't want to append the values of both variables but compare the two arrays and show in green the ones that are present in both arrays.

I'm trying to do that with a IF in a Foreach but the output is always White.

foreach ($element in $from) {
    if($element -contains $newuser) {
        write-host $element `n -ForegroundColor Green
    } else {
        write-host $element `n -ForegroundColor White
    }
}

Need some help to figure why.

Thank you!

Upvotes: 1

Views: 291

Answers (1)

Mark Elvers
Mark Elvers

Reputation: 647

In addition to the typo that Doug mentioned your if clause is reversed. Try it like this:

if ($newuser -contains $element) { ... }

Upvotes: 1

Related Questions