36 C
Thursday, May 23, 2024

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 Manoharan
Satheshwaran Manoharanhttps://www.azure365pro.com
Award-winning Technology Leader with a wealth of experience running large teams and diversified industry exposure in cloud computing. From shipping lines to rolling stocks.In-depth expertise in driving cloud adoption strategies and modernizing systems to cloud native. Specialized in Microsoft Cloud, DevOps, and Microsoft 365 Stack and conducted numerous successful projects worldwide. Also, Acting as a Technical Advisor for various start-ups.

Related Articles


Please enter your comment!
Please enter your name here

× How can I help you?