21.9 C
Wednesday, January 19, 2022

Automate Changing UPN equals Email with a simple script

For Autodiscover to work properly in On premises and Exchange Online , We need email and User principal name to match. In the start of the project we have to do it once.

But for new users every time administrators cannot keep checking if its equal or not. it would be tiring to do it manually everytime. So if require you can run it on a task scheduler so that it maintains the UPN and Email to be same for the ones which is not matching.

Supported on Exchange 2013 or above | Premise or Exchange Hybrid Server

NOTE : Before running the script run below commands to check which are the mailboxes it will apply to

Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname}

To Check its running with Whatif

Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname} | ForEach-Object {Set-Mailbox $_.identity -UserPrincipalName $_.Primarysmtpaddress -whatif}

To Apply

Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname} | ForEach-Object {Set-Mailbox $_.identity -UserPrincipalName $_.Primarysmtpaddress}

Download Change_UPN_equals_Email.ps1

Task Scheduler


Create Basic Task

Choose Daily


Set a time


Start a Program


  • Powershell
  • C:\Scripts\Change_UPN_equals_Email.ps1

Download Change_UPN_equals_Email.ps1


Made to stop the task if it exceeds 4 hours


Download Change_UPN_equals_Email.ps1

# NOTE : Before running the script run below commands to check which are the mailboxes it will apply to
# Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname}
# Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname} | ForEach-Object {Set-Mailbox $_.identity -UserPrincipalName $_.Primarysmtpaddress -whatif}

# Include Exchange Powershell Module
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

#Lists All Mailboxes
#Checks Email and UPN are same
#Lists which are not email
#Applies UPN Matching email

Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname} | ForEach-Object {Set-Mailbox $_.identity -UserPrincipalName $_.Primarysmtpaddress}

# Exit Exchange Powershell Module
Remove-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

Download Change_UPN_equals_Email.ps1

Known Errors –  On Exchange 2010 it cannot take two pipelines together.

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
+ CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace)
+ FullyQualifiedErrorId : RemotePipelineExecutionFailed

To Avoid two pipelines. Save to variable and run the same.

$a = Get-Mailbox -ResultSize Unlimited | Where-Object {$_.Primarysmtpaddress -ne $_.UserPrincipalname}
$a | ForEach-Object {Set-Mailbox $_.identity -UserPrincipalName $_.Primarysmtpaddress -whatif}
$a | ForEach-Object {Set-Mailbox $_.identity -UserPrincipalName $_.Primarysmtpaddress}
Satheshwaran Manoharanhttps://www.azure365pro.com
Satheshwaran Manoharan is an Microsoft Office Server and Services MVP , Publisher of Azure365pro.com. Specialized in Office365 / Microsoft Exchange / Virtualization , Sathesh is an Messaging Expert supporting/Designing/Deploying many medium size businesses to large enterprises when it comes to Corporate messaging and Virtualization Infrastructure

Related Articles


Please enter your comment!
Please enter your name here