邮件不为空的 Get-AdUser

Get-AdUser where mail is not null

我正在尝试获取 AD 中拥有电子邮件(邮件属性)的所有用户的列表。我有这个命令

Get-AdUser -filter * -Properties mail | Select SAMAccountName, mail | Export-CSV -Path $userPath -NoTypeInformation

问题是我不知道如何限制或过滤掉电子邮件为 null/blank 的用户。我不在乎脚本有多复杂,因为它将成为更大的 powershell 脚本的一部分。如果解决方案是循环遍历 CSV,那是一种选择,但更喜欢更快的方法。有人知道怎么做吗?

试试这个:

Get-ADUser -Properties mail -Filter {mail -like '*'}

试试这个:

Get-ADUser -Properties mail | where {$_.mail -ne $null} | Select SAMAccountName, mail | Export-CSV -Path $userPath -NoTypeInformation

问题已经得到解答,但对我来说,最简单的就是依靠 LDAP 过滤器语法。例如:

 Get-ADUser -LDAPFilter "(mail=*)"

“=*”是标准的 LDAP 运算符:

* (wildcard)

You use the wildcard operator to represent a value that could be equal to anything. One such situation might be if you wanted to find all objects that have a value for title. e for title. You would then use:

(title=*)

来源LDAP Query Basics