Dave Herrell - Blog - IT Toolbox - PowerShell Banner

PowerShell Basic: Get List of all MS365 emails address & alias

If you work directly out of Entra ID & MS365, or even setup with Hybrid environment, you may need to audit all active email address along with email alias.  If you only create your address and alias through Active Directory, you can always use this technique to search AD.  However, in some enviroments, especially those Hybrid that utilize MS Teams and other cloud groups, this option wont work.  

Below is a script that will connect to the EOM (Exchange Online Management) via PowerShell, scan for all emails and alias.  This will include MS Teams Groups, cloud based, AD sync’d and shared mailboxes.  Then exports them in a nice CSV file for you.

First, make sure you have the EOM module installed within your PowerShell environment.

				
					Install-Module -Name ExchangeOnlineManagement
				
			

Next you’ll run the script below.  This will prompt you for your MS365 username and password and will work with accounts that utilize 2-factor.   Reminder, replace the CSV export path with your own.

				
					# Import the EOM module
Import-Module ExchangeOnlineManagement

# Connect to Exchange Online using your credentials
# Replace 'username@domain.com' with your actual credentials
Connect-ExchangeOnline -UserPrincipalName "username@domain.com"

# Get all mailboxes and aliases
$mailboxes = Get-Recipient -ResultSize Unlimited | Select-Object DisplayName, RecipientType, @{Name="EmailAddresses";Expression={($_.EmailAddresses | Where-Object { $_ -match "^smtp:" } | ForEach-Object {$_ -replace "smtp:",""}) -join "," }}

# Export the results to a CSV file
$mailboxes | Export-Csv -Path C:\Users\dave\Desktop\MS365_EmailAddresses.csv -NoTypeInformation

# Disconnect from Exchange Online
Disconnect-ExchangeOnline

# Informative message
Write-Host "All MS365 email addresses and aliases exported to 'MS365_EmailAddresses.csv'"

				
			

Depending on the size of your organization, this may take a while to pull.  It’ll appear to be “hung” on the “This V3 EXO PowerShell…..” screen for a bit, just have patience padawan. You may get a prompt to accept, choose “Yes to All” so it completes the scan.

After this complete, you should receive the confirmation along with the CSV file saved to your chosen location.

				
					 PS C:\Windows\system32> # Import the EOM module
Import-Module ExchangeOnlineManagement

# Connect to Exchange Online using your credentials
# Replace 'username@domain.com' with your actual credentials
Connect-ExchangeOnline

# Get all mailboxes and aliases
$mailboxes = Get-Recipient -ResultSize Unlimited | Select-Object DisplayName, RecipientType, @{Name="EmailAddresses";Expression={($_.EmailAddresses | Where-Object { $_ -match "^smtp:" } | ForEach-Object {$_ -replace "smtp:",""}) -join "," }}

# Export the results to a CSV file
$mailboxes | Export-Csv -Path C:\Users\dave\Desktop\MS365_EmailAddresses.csv -NoTypeInformation

# Disconnect from Exchange Online
Disconnect-ExchangeOnline

# Informative message
Write-Host "All MS365 email addresses and aliases exported to 'MS365_EmailAddresses.csv'"

----------------------------------------------------------------------------------------
This V3 EXO PowerShell module contains new REST API backed Exchange Online cmdlets which doesn't require WinRM for Client-Server communication
. You can now run these cmdlets after turning off WinRM Basic Auth in your client machine thus making it more secure. 

Unlike the EXO* prefixed cmdlets, the cmdlets in this module support full functional parity with the RPS (V1) cmdlets.

V3 cmdlets in the downloaded module are resilient to transient failures, handling retries and throttling errors inherently. 

REST backed EOP and SCC cmdlets are also available in the V3 module. Similar to EXO, the cmdlets can be run without WinRM basic auth enabled. 


For more information check https://aka.ms/exov3-module
----------------------------------------------------------------------------------------

All MS365 email addresses and aliases exported to 'MS365_EmailAddresses.csv'

PS C:\Windows\system32>  

				
			

That’s all there is to it!  I’ve found this script handy for orgs that have had years of mergers and acquisitions that resulted in years worth of alias adds and shared mailboxes.  

Hope you find this helpful!