Why?
So, you want to know every single email address in a particular Office 365 tenant… How do you go about listing all of the users aliases, distribution lists, and other possible places to email?
The following code will produce a CSV with all of the possible email addresses on the tenant, including DisplayName, RecipientType, PrimarySMTPAddress,EmailAddress and AddressType. It will also pick up all Dynamic Distribution Group members.
Note that ‘SMTP’ uppercase will always denote the Primary SMTP Address.
Code
# Connect to Exchange Online
Connect-ExchangeOnline
# Initialize an empty List to store the processed data
$EmailDetailsList = New-Object System.Collections.Generic.List[PSObject]
# Get all recipients
$RecipientDetails = Get-Recipient -ResultSize Unlimited | Select-Object DisplayName, RecipientType, PrimarySmtpAddress, EmailAddresses
# Loop through each recipient and break out email addresses on separate lines
foreach ($Recipient in $RecipientDetails) {
foreach ($Email in $Recipient.EmailAddresses) {
# Extract the address type and the actual email address
$AddressType, $ActualEmailAddress = $Email -split ":", 2
# Create a custom object for each email address
$RecipientObject = [pscustomobject]@{
DisplayName = $Recipient.DisplayName
RecipientType = $Recipient.RecipientType
PrimarySmtpAddress = $Recipient.PrimarySmtpAddress
EmailAddress = $ActualEmailAddress
AddressType = $AddressType
}
# Add the custom object to the list
$EmailDetailsList.Add($RecipientObject)
}
}
# Get all dynamic distribution groups
$DynamicDistributionGroups = Get-DynamicDistributionGroup
# Loop through each dynamic distribution group
foreach ($DDG in $DynamicDistributionGroups) {
# Evaluate the membership of the dynamic distribution group
$Members = Get-Recipient -RecipientPreviewFilter $DDG.RecipientFilter
# Loop through each member and add the dynamic group email as an additional address
foreach ($Member in $Members) {
# Create a custom object for each member that includes the dynamic group's address as an additional email
$MemberObject = [pscustomobject]@{
DisplayName = $Member.DisplayName
RecipientType = $Member.RecipientType + " (Dynamic Group Member)"
PrimarySmtpAddress = $Member.PrimarySmtpAddress
EmailAddress = $DDG.PrimarySmtpAddress
AddressType = "DynamicGroup"
}
# Add the custom object to the list
$EmailDetailsList.Add($MemberObject)
}
}
# Export the processed data to a CSV file
$EmailDetailsList | Export-Csv -Path "Office365Recipients_Expanded.csv" -NoTypeInformation
# Disconnect after completion
Disconnect-ExchangeOnline -Confirm:$false
Buying Office 365 or Azure and dissatisfied with MS Support? Move your existing Office 365 to us and we will support you every step of the way — reach out and get started today!