PowerShell: script to get all SMTP Email addresses from Office 365


If you want to get all Email addresses from Office 365, it would involve reading the proxy address field and breaking it out. The script below does that.


If you are going to run this on a schedule then you may want to save the Tenant password else you will be prompted to entered it.

  1. Create a folder called O365Info on the server where you will run the script regularly
  2. Open a PowerShell window and run this
#Don’t change anything copy and run as is

Read-Host -Prompt “Mypassword” -AsSecureString | ConvertFrom-SecureString | Out-File “C:\O365Info\tenantpassword.key”

You will be prompted for a password, enter it and the password will be encrpted and stored in the file

The code

#Import the O365 library

Import-Module MSOnline

$TenantUname = “xyz@domain.onmicrosoft.com”

#reference where you stored the Tenant password

$TenantPass = cat “D:\O365Info\tenantpassword.key” | ConvertTo-SecureString

$TenantCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $TenantUname, $TenantPass

#Open a session to Exchange on O365

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $TenantCredentials -Authentication Basic -AllowRedirection

Import-PSSession $Session -AllowClobber

Connect-MsolService -Credential $TenantCredentials

#Declare a file where you will store the proxy address data

$EmailTempfile = “D:\O365Info\O365Emailproxy.csv”

$fileObjectEmail = New-Item $EmailTempFile -type file -force

#Declare a file to store the email info

$fileName = “D:\O365Info\O365EmailAddresses.csv”

$fileObject = New-Item $fileName -type file -force

#Write the header

$evt_string = “Displayname,EmailAddresses”

$evt_string | Out-file $fileObject -encoding ascii -Append

#Get the proxy info

Get-Recipient -Resultsize unlimited -RecipientType Usermailbox | select DisplayName,EmailAddresses | Export-csv -notypeinformation $fileObjectEmail -append -encoding utf8

#Read output into a variable

$file = Get-Content $EmailTempfile

for($i=1;$i -lt $file.count;$i++){


#Split the proxy data into individual addresses

$csvobj = ($file[$i] -split “,”)

$EmailAddr = $csvobj[1]

$GetEmail = $EmailAddr -replace ‘”‘, ” -split(‘ ‘)

#write out the display name and email address (One person can have several), filter for smtp only and exclude onmicrosoft.com addresses

for($k=0;$k -lt $GetEmail.count;$k++){

If (($GetEmail[$k] -match “smtp:”) -and ($GetEmail[$k] -notmatch “onmicrosoft.com”)){

$evt_string = $csvobj[0]+”,”+$GetEmail[$k].split(“:”)[1]

$evt_string | Out-file $fileObject -encoding utf8 -Append




One thought on “PowerShell: script to get all SMTP Email addresses from Office 365

  1. Pingback: MIM 2016: Creating AD accounts in O365 Exchange Hybrid environments | tlktechidentitythoughts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s