Get-AdUser where mail is not null

2020-07-23 03:06发布

I am trying to get a list of all users in AD that have an email (mail attribute). I have this command

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

The problem is that I do not know how to limit or filter out users where the email is null/blank. I do not care how complex the script is as it will be part of a much larger powershell script. If the solution is to loop through the CSV that is an option but would prefer something quicker. Does anyone know how to do this?

3条回答
迷人小祖宗
2楼-- · 2020-07-23 03:44

Try this:

Get-ADUser -Properties mail | where {$_.mail -ne $null} | Select SAMAccountName, mail | Export-CSV -Path $userPath -NoTypeInformation
查看更多
Summer. ? 凉城
3楼-- · 2020-07-23 03:46

Try this:

Get-ADUser -Properties mail -Filter {mail -like '*'}
查看更多
兄弟一词,经得起流年.
4楼-- · 2020-07-23 04:04

The question has already be answered, but for me, the simplest is to rely on LDAP filter syntax. For instance:

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

The "=*" is a standard LDAP operator:

* (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=*)

Source LDAP Query Basics

查看更多
登录 后发表回答