PowerShell: Script to get all SMTP email addresses from Office 365

Introduction

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.

Preamble

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 enter 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 encrypted 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++){

$evt_string=""

 

#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

}

}

}