Powershell字符串不包含

Powershell string does not contain

我有一些接受字符串的代码,

Foreach($user in $allUsers){
    if($user.DisplayName.ToLower().Contains("example.com") -or $user.DisplayName.ToLower()) {
    } else {
        $output3 = $externalUsers.Rows.Add($user.DisplayName)
    }
}

if 紧跟在 -or 之后的部分 我需要检查字符串是否不包含 @ 符号。我如何检查@符号是否丢失?

有一百万种方法,出于可读性考虑,我可能会选择以下方法:

$user.DisplayName -inotmatch "@"

-match 运算符使用右侧的模式对左侧操作数进行正则表达式匹配。

i作为前缀使其明确区分大小写i,并且not前缀否定表达式

你也可以这样做:

-not($user.DisplayName.ToLower().Contains("@"))
or
!$user.DisplayName.ToLower().Contains("@")

对于简单的通配符文本匹配(也许你讨厌正则表达式,我知道什么?):

$user.DisplayName -notlike "*@*"

或者寻找带 IndexOf 的子串;

$user.DisplayName.IndexOf("@") -eq (-1)