Powershell 阵列输出多种颜色的 somes 值

Powershell Array output somes values with multiple colors

我有两个包含电子邮件地址的数组:

我想将 $from 的值输出给用户,但我想为变量 $newuser 中也存在的值着色。

因此输出将以白色显示 $from 中的所有电子邮件地址,但以绿色显示 $newuser 中存在的电子邮件地址。

我不想附加两个变量的值,而是比较两个数组并以绿色显示两个数组中都存在的值。

我正在尝试在 Foreach 中使用 IF,但输出始终为白色。

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

需要一些帮助来弄清楚原因。

谢谢!

除了 Doug 提到的打字错误之外,您的 if 子句被颠倒了。像这样尝试:

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