Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two arrays which contain email addresses:

  • $from
  • $newuser

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!

question from:https://stackoverflow.com/questions/65944232/powershell-array-output-somes-values-with-multiple-colors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
349 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...