In preparation for a Microsoft 365 migration, we often need to get the list of existing users, mail contacts and distribution groups from Exchange. The following powershell command will simply get all recipients and email aliases as well the the type and export to CSV.

Note: that this needs to run from Exchange Management Shell

Get-Recipient -ResultSize unlimited | Sort RecipientType | Select DisplayName,PrimarySmtpAddress, @{n='EmailAddresses';e={[string]::join(";",($_.Emailaddresses))}}, @{n='MailboxLastLogon';e={Get-MailboxStatistics $_.Identity | select -expand LastLogonTime}}, RecipientType | Export-CSV -Path "C:\temp\Mailboxes.csv" -NoTypeInformation