Export Office 365 Email Addresses using PowerShell

You can use the Exchange PowerShell command Get-Mailbox to retrieve the primary email address and secondary (or alias) email addresses for all mailboxes (eg: UserMailbox, SharedMailbox, etc..). We can also use the Get-Recipient cmdlet to get email addresses for all mail-enabled objects (for example, mailboxes, mail users, mail contacts, unified groups, and distribution groups). In this post, I am going to explain export office 365 users email addresses using PowerShell.

Run the below command to export the primary and alias email addresses for all user mailboxes.

Get-Mailbox -ResultSize Unlimited |
Select-Object DisplayName,PrimarySmtpAddress, @{Name="AliasSmtpAddresses";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | ForEach-Object {$_ -replace "smtp:",""}) -join "," }}  |
Export-Csv "C:\Email-Addresses.csv" -NoTypeInformation -Encoding UTF8

The above command exports only user mailboxes and shared user mailboxes, if you want to export email address all mail-enabled objects, then we need to use Get-Recipient cmdlet.

Get-Recipient -ResultSize Unlimited |
Select-Object DisplayName,RecipientType, PrimarySmtpAddress, @{Name="AliasSmtpAddresses";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | ForEach-Object {$_ -replace "smtp:",""}) -join "," }}  |
Export-Csv "C:\Email-Addresses.csv" -NoTypeInformation -Encoding UTF8

If you use Exchange Online PowerShell V2 module, then you can use new equivalent commands Get-EXOMailbox and Get-EXORecipient.

Get-EXOMailbox -ResultSize Unlimited |
Select-Object DisplayName,PrimarySmtpAddress, @{Name="AliasSmtpAddresses";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | ForEach-Object {$_ -replace "smtp:",""}) -join "," }}  |
Export-Csv "C:\Email-Addresses.csv" -NoTypeInformation -Encoding UTF8

Leave a comment