Monday, 3 February 2020

Export All Email Addresses from Office 365 using Powershell

We can use the Exchange Powershell command Get-Mailbox to retrieve the primary email address and secondary (or alias) email addresses for all mailboxes (ex: 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).

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