Exchange
| |

Bulk Target Address Amendment

Recently after performing an M365 migration due to an acquisition I needed to perform a Bulk Target Address Amendment for all M365 Users. Due to the old on.microsoft tenant the old address was set to, the users had been removed. Now if you only have a few users this would not be a major problem. However, the migration I was undertaking had in excess of 2300 users. The below blog post, should guide you how to complete this task. Should the PowerShell script not meet requirements. Then it can amended to suite the criteria required for your migration but any amendments should be quicker than creating a whole new script.

Bulk Target Address Amendment

How To: Bulk Target Address Amendment

Firstly, the address policy required amendment to include the new domain. In my example I created an address policy for Alias@TheTechEvolution.mail.onmicrosoft.com

However, the reason for this was not all users had a first name . last name and some conflicts would have existed. Secondly we already had a unique identifier in Mail Nick Name which saved effort when this was already immutable. Once the address policy applied it was possible to run the below script.

Now all that need amending thing in the script for this to work is the below attributes for your requirments.

$domain

$SearchOU

The script should search for all users in the search scope and child OUs. Where they have a mailnickname. Or meet the following criteria Provisions User Mailbox, Provision Shared Mailbox or Provision Remote Room Mailbox. If you have any other requirements like Migrated users than you can add these in and chain them as per your requirement.

$domain=’@TheTechEvolution.mail.onmicrosoft.com’
$Outputfile= "C:\temp\1.csv"
$sTimestamp = Get-Date -Format "yyyyMMddHHmmss"
$SearchOU = "OU=Users,DC=TheTechEvolution,DC=com"
$Headings="Status, Timestamp, Comment" | Out-file $Outputfile -Encoding utf8 -Append -ErrorAction "Stop"

#Grab the Users
Write-Host "Get all users with a populated MailNickName if it does not have a mailnickname the account in theory shouldn't not be Exchange Enabled so we should not care."
$data = get-aduser -Filter * -SearchBase $SearchOU -Properties * | Where-Object {$_.MSExchRemoteRecipientType -eq '1' -or $_.MSExchRemoteRecipientType -eq '97' -or $_.MSExchRemoteRecipientType -eq '33' -and $_.MailNickName -ne $Null}| Select-Object MailNickName,TargetAddress,UserPrincipalName,samaccountname,MSExchRemoteRecipientType

Write-Host "Captured"$($data.count)"accounts to amend"

#Check the Data contains Users and quit if not since nothing is being done.
if ($null -eq $data)  {
"write-output No Users Found"
$LogMSG= ("Failed," + "$($sTimestamp)" +", No Users Found")
$LogMSG | Out-file $Outputfile -Encoding utf8 -Append -ErrorAction "Stop"
Exit
Else
{Write-Host" Data has been found"
}
}

Foreach
($User in $Data) { $TargetAddress="SMTP:$($User.MailNickName + "$($Domain)")"
Try
{
Set-ADUser $User.SamAccountName -Replace @{TargetAddress="$($TargetAddress)"}
Write-host "Adding "$($TargetAddress)" to "$($User.SamAccountName)" TargetAddress"
$LogMSG= ("Success," + "$($sTimestamp)" +",Added " + "$($TargetAddress)" + " To Account " + "$($User.SamAccountName)")
$LogMSG | Out-file $Outputfile -Encoding utf8 -Append -ErrorAction "Stop"
Get-ADUser $User.SamAccountName -Properties *| fl TargetAddress
}
Catch {
Write-host "Error Adding "$($TargetAddress)" to "$($User.SamAccountName)" TargetAddress"
$LogMSG= ("Failed," + "$($sTimestamp)" +",To Add " + "$($TargetAddress)" + " To Account " + "$($User.SamAccountName)")
$LogMSG | Out-file $Outputfile -Encoding utf8 -Append -ErrorAction "Stop"
}
Remove-Variable User, TargetAddress, LogMSG -ErrorAction SilentlyContinue
}

Write-Host "Please review '$($OutputFile)' and filter Failures in excel and resolve as applicable"

Please not the script above comes with no warranty and we accept not responsibilities for the actions taken during running this script.

Additional Details: Exchange

If you want to find more details please visit the attached tech net article from Microsoft for more details.

If you want to find more details out on Exchange Online please check out my other blog posts.

Please feel free to drop a comment below or share this blog post if it has helped you.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *