You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell中过滤Get-ADUser结果的空邮箱与空经理字段?

PowerShell AD组用户信息提取:同时过滤空邮箱和空经理字段

其实Where-Object(别名where)完全支持同时设置多个过滤条件,用逻辑运算符-and就能组合多个判断,实现只保留邮箱和经理字段都不为空的记录。

另外建议不要用properties *,因为会加载AD用户的所有属性,效率很低,只指定需要的EmailAddress和Manager即可。修改后的代码如下:

Get-ADGroupMember -Identity "groupname" -Recursive |
    Get-ADUser -Properties EmailAddress, Manager |
    Where-Object { $_.EmailAddress -ne $null -and $_.Manager -ne $null } |
    Select-Object DisplayName, EmailAddress, @{
        Name = "ManagerEmail"
        Expression = { (Get-ADUser -Property EmailAddress $_.Manager).EmailAddress }
    } |
    Export-Csv -Path c:\data.csv -NoTypeInformation

关键说明:

  • Get-ADUser -Properties EmailAddress, Manager:仅获取需要的属性,避免加载冗余数据,提升执行效率
  • Where-Object { $_.EmailAddress -ne $null -and $_.Manager -ne $null }:通过-and同时校验两个字段,只保留都不为空的用户记录
  • 经理邮箱的表达式部分,因为已经通过Where过滤掉了Manager为空的记录,所以无需额外判断经理对象的有效性

内容的提问来源于stack exchange,提问作者catran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 04:20:25