Check SMTP Addresses of On Premise Mailboxes
function checkSmtpAddresses{
$microsoftDomain1="@kimconnect.mail.onmicrosoft.com"
$microsoftDomain2="@kimconnect.onmicrosoft.com"
$dotLocal=".local"
$onpremMailboxes=Get-Mailbox -ResultSize unlimited | Select-Object Alias,@{Name="Domain";Expression={$_.PrimarySmtpAddress.Domain}}
foreach ($item in $onpremMailboxes){
$output="";
$username=$item.Alias
$mailbox = Get-Mailbox -Identity $username
$smtpAddresses=$mailbox.EmailAddresses | Where-Object {$_.SmtpAddress -ne $null} | select smtpaddress
$matchDotLocal=$smtpAddresses | Where-Object {$_ -match $dotLocal}
$matchMicrosoftDomain1=$smtpAddresses | Where-Object {$_ -match $microsoftDomain1}
$matchMicrosoftDomain2=$smtpAddresses | Where-Object {$_ -match $microsoftDomain2}
if($matchDotLocal){$output="dotLocal domain is detected for this mailbox. Please consider removing it. "}
if($matchMicrosoftDomain1){$output+="$microsoftDomain1 domain detected. "}
if($matchMicrosoftDomain2){$output+="$microsoftDomain2 domain detected. "}
if(!($matchMicrosoftDomain1) -and !($matchMicrosoftDomain2)){$output+="this account has no SMTP email addresses. "}
"$username`: $output"
}
}
Sample Output:
testaccount1: @kimconnect.mail.onmicrosoft.com domain detected.
testaccount2: @kimconnect.mail.onmicrosoft.com domain detected.
testaccount3: @kimconnect.mail.onmicrosoft.com domain detected.
Fix SMTP Addresses:
# This script fixes all on premise mailboxes to prepare them for migration to Office 365
# 1. Remove non-routable dot local domainSuffix
# 2. Add Microsoft Email addresses
# 3. Replace email address using an old domain suffix with a new domain suffix

# Set variables here
$microsoftDomain1="kimconnect.mail.onmicrosoft.com"
$microsoftDomain2="kimconnect.onmicrosoft.com"
$dotLocal=".local"
$oldDomain="kimconnect.org"
$newDomain="kimconnect.com"

function generateBatch{
"Generating batch by collecting information on all mailboxes..."
$GLOBAL:batch=Get-Mailbox -ResultSize unlimited | Select-Object Alias,@{Name="FixedPrimaryEmail";Expression={if($_.PrimarySmtpAddress.Domain -like
$oldDomain){"$Alias@$newDomain"}else{$_.PrimarySmtpAddress} }}

# Test with 2 records
#$GLOBAL:batch=Get-Mailbox -ResultSize 2 | Select-Object Alias,@{Name="FixedPrimaryEmail";Expression={if($_.PrimarySmtpAddress.Domain -like #$oldDomain){"$Alias@$newDomain"}else{$_.PrimarySmtpAddress} }}
}

function setPrimarySMTP ($alias,$newEmail){
Set-Mailbox $alias -EmailAddresses @{add="$alias@$newDomain"}
$mailbox = Get-Mailbox -Identity $alias
$primaryDomainCurrent=$mailbox.PrimarySmtpAddress.Domain
"Comparing existing email domain with $primaryDomainCurrent if it matches $oldDomain. If so set desired email as $newEmail..."

if ($primaryDomainCurrent -like $oldDomain){
"Primary SMTP email adddress for this user is $primaryDomainCurrent. Updating email address to $newEmail... "
$mailbox.EmailAddresses += $newEmail
$mailbox | Set-Mailbox -EmailAddressPolicyEnabled $False -PrimarySmtpAddress $newEmail
}else{
"Primary SMTP email address is already matching the desired domain. Skipping this record."
}
}

function removeDotLocal($alias){
$mailbox = Get-Mailbox -Identity $alias
$smtpAddresses=$mailbox.EmailAddresses | Where-Object {$_.SmtpAddress -ne $null} | select smtpaddress
$matches=$smtpAddresses | Where-Object {$_ -match $dotLocal}
if ($matches){
foreach ($match in $matches){
$matchedDotLocal=$match.SmtpAddress
"Removing $matchedDotLocal..."
Set-Mailbox $alias -EmailAddressPolicyEnabled $False -EmailAddresses @{remove="$matchedDotLocal"}
}
}
}

function addMicrosoftEmails{
Set-Mailbox $username -EmailAddressPolicyEnabled $False -EmailAddresses @{add="$username@$microsoftDomain1"}
Set-Mailbox $username -EmailAddressPolicyEnabled $False -EmailAddresses @{add="$username@$microsoftDomain2"}
}

function fixEmailAliases{
generateBatch;
foreach ($item in $batch){
$username=$item.Alias
$primaryEmail=$item.FixedPrimaryEmail
"Processing $username with primary email as $primaryEmail..."
addMicrosoftEmails;
removeDotLocal $username;
setPrimarySMTP $username $primaryEmail;
}
}
fixEmailAliases;
First Draft Version:

Make a batch file named “C:\Users\kimconnect\Desktop\batch1.csv”:

Display Name,Alias,Primary SMTP Address,EmailUsername,Domain,FixedPrimaryEmail
Kim Connect,kconnect,[email protected],kconnect,kimconnect.org,[email protected]
Bruce Lee,blee,[email protected],blee,kimconnect.org,[email protected]
Chuck Norris,cnorris,[email protected],cnorris,kimconnect.org,[email protected]

Run this script As Administrator in Exchange PowerShell:

$batchfile="C:\Users\kimconnect\Desktop\batch1.csv"
$dotLocal=".local"
$domain="kimconnect"
$domainSuffix=".com"
$primarySmtpDomain="kimconnect.com"
$oldDomain="kimdisconnect.org"

$batch=import-csv $batchfile

function setPrimarySMTP ($alias,$newEmail){
$mailbox = Get-Mailbox -Identity $alias
$primaryDomainCurrent=$mailbox.PrimarySmtpAddress.Domain
"Comparing existing email domain with $primaryDomainCurrent if it matches $oldDomain. If so set desired email as $newEmail..."

if ($primaryDomainCurrent -like $oldDomain){
"Primary SMTP email adddress for this user is $primaryDomainCurrent. Updating email address to $newEmail... "
$mailbox.EmailAddresses += $newEmail
$mailbox | Set-Mailbox -EmailAddressPolicyEnabled $False -PrimarySmtpAddress $newEmail
}else{
"Primary SMTP email address is already matching the desired domain. Skipping this record."
}
}

function removeDotLocal($alias){
$mailbox = Get-Mailbox -Identity $alias
$smtpAddresses=$mailbox.EmailAddresses | Where-Object {$_.SmtpAddress -ne $null} | select smtpaddress
$matches=$smtpAddresses | Where-Object {$_ -match $dotLocal}
if ($matches){
foreach ($match in $matches){
$matchedDotLocal=$match.SmtpAddress
"Removing $matchedDotLocal..."
Set-Mailbox $alias -EmailAddresses @{remove="$matchedDotLocal"}
}
}
}

function fixEmailAliases{
foreach ($item in $batch){
$username=$item.Alias
$primaryEmail=$item.FixedPrimaryEmail
"Processing $username with primary email as $primaryEmail..."

Set-Mailbox $username -EmailAddresses @{add="$username@$domain.onmicrosoft.com"}
Set-Mailbox $username -EmailAddresses @{add="$username@$domain.mail.onmicrosoft.com"}
Set-Mailbox $username -EmailAddresses @{add="$username@$domain$domainSuffix"}
removeDotLocal $username
setPrimarySMTP $username $primaryEmail

}
}
fixEmailAliases;