Export email aliases from users in Active Directory

In an on-premise Active Directory, under the attributes section, there is a field called ProxyAddresses, which is basically a multi-valued field listing all the email addresses of an account.

The primary email is written as SMTP (with capital letters) and all other alternative email addresses are written as smtp (with lower case letters).

In this example we want to use a PowerShell script, to export all these email addresses to a CSV file, which we then can open in Excel.

We will use some things you might not necessarily need, like for example NoTypeInformation (which will remove #TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser) from the CSV file. We also use -Encoding Unicode which will change the character encoding to UTF-16 format, which we need for nordic letters in the user names.

$env:USERPROFILE\Desktop\ is a variable we use to export the results to the currently logged on users desktop folder. And $(get-date -f yyyy-MM-dd-HH-mm-ss) adds a timestamp to the filename for example 2022.01.29.

Remember to change ABCompany and the other variables to match your Active Directory’s organizational units. Also, I like to apply employeeID -like "*" so I can limit the search to users that have an employee number.

# Export users in Active Directory to text file or CSV

# Drilldown search a spesific OU folder
$searchScope = "OU=Users,OU=ABCompany,DC=abc,DC=local"

# Search with spesific filter
$searchFilter = {
    (employeeID -like "*") 
    -And (Enabled -eq $true) 
}

# Run the command
Get-ADUser -Credential abc.local\Administrator -server servername.abc.local -SearchBase $searchScope -Filter $searchFilter -Properties proxyaddresses | Select-Object Name, @{L = "ProxyAddresses"; E = { $_.ProxyAddresses -join ";"}} | Export-Csv -Force -Path $env:USERPROFILE\Desktop\$(get-date -f yyyy-MM-dd-HH-mm-ss)-ProxyAddresses.CSV -NoTypeInformation -Delimiter ";" -Encoding UTF8

This will give you something like:

"Name","ProxyAddresses"
"john Smith","SMTP:jsmith@ABCompany.onmicrosoft.com;smtp:john@ABCompany.com;smtp:john@ABCompany.jp;smtp:john.smith@ABCompany.mail.onmicrosoft.com;SMTP:john.smith@ABCompany.fr;smtp:john.smith@ABCompany.com;smtp:john.smith@ABC.no

There are two downsides here.

We only get two CSV headers "Name" and "ProxyAddresses". We don’t get "ProxyAddresses" for each mail alias listed. So reading the results are a bit hard.

The other downside is that we also export the "x500:/o=ExchangeLabs/ou=Exchange Administrative Group" which is probably not needed. But I guess it is better to have too much information than too little.

I have not really found a solution for that yet…

Leave a Reply

Your email address will not be published. Required fields are marked *