Microsoft released Cumulative Update 9 for Exchange Server 2019 (KB4602570) on March 16, 2021. Also…
Change Users UPN automatically with scheduled task
How to combine PowerShell with a scheduled task to change Users UPN automatically? Why do we want to change Users UPN automatically? First, I always say that if you can automate the process, you should do it. Second, all the accounts that we create in Active Directory need the correct UPN suffix. When you don’t add the correct UPN to the user account, it will not sync with Azure, which leads to problems. By automating the task, you make sure that it will automatically change the Users UPN and sync with Azure AD Connect to Azure. In this article, you will learn how to change Users UPN automatically with a scheduled task.
Table of contents
Before creating the change Users UPN scheduled task
First, you need to add a UPN suffix in Active Directory. The next step is to download the ChangeUPN.ps1 script and place it in the C:\Scripts\ folder. Create a scripts folder if you don’t have one in the C:\ drive.
In my example, all the users in Active Directory with UPN exoip.local will change to exoip.com. Edit the values to your needs. Read more on how to Change Users UPN with PowerShell.
$LocalUsers = Get-ADUser -Filter {UserPrincipalName -like '*exoip.local'} -Properties userPrincipalName -ResultSetSize $null
$LocalUsers | foreach {$newUpn = $_.UserPrincipalName.Replace("exoip.local","exoip.com"); $_ | Set-ADUser -UserPrincipalName $newUpn}
After creating the folder and placing the script, it should look like the following.
Automate the change Users UPN with task scheduler
After we confirmed that the ChangeUPN.ps1 script is in place, we can start automating the change Users UPN script.
Start Task Scheduler.
Click the Task Scheduler Library and click New folder… Name the folder Exchange and click OK.
Click the created Exchange folder. Click Create Task…
We have three tabs that we are going to fill in. The tabs are General, Triggers, Actions, and Settings.
- We start with the first tab General.
- Name: ChangeUPN.
- Description: This script will run every night. It will check if there are accounts configured in AD with UPN suffix exoip.local. If yes, it will change these accounts to UPN suffix exoip.com.
- An account with privileges for running the task.
- Click on Run whether user is logged on or not.
- Enable checkbox Run with highest privileges.
- Configure for Windows Server 2016 if you have a Windows Server 2016. If you have another Windows Server version running, select that from the dropdown menu.
Click the tab Triggers. Click New…
Select the settings Daily. Configure which time the script needs to start running, for example, 02:00:00. Make sure that it’s Enabled and click OK.
Click the tab Actions. Click New…
Make sure to copy and paste below to both of the fields.
Program/script: Powershell.exe
Add arguments (optional): -ExecutionPolicy Bypass -File “C:\Scripts\ChangeUPN.ps1”
Click OK.
Click the tab Settings. Change the option stop the task if it runs longer than to 4 hours. Click OK.
Fill in the credentials for running this task daily and click OK.
The change Users UPN task is scheduled. From now on, the task scheduler will automatically change Users UPN. In the next step, you will run the script to test if all is working great.
Run the change Users UPN task in task scheduler
Before running the change Users UPN task
Go to Active Directory and Users (ADUC) and open a user account with the old UPN suffix.
Click the created task UsersUPN and click Run. The Last Run Result will show that The operation completed successfully. (0x0).
After running the change Users UPN task
Refresh or restart ADUC and go to the user. Verify that the UPN suffix is successfully changed.
The UPN suffix is automatically changed.
Read more: Cleanup Exchange logs automatically with scheduled task »
Conclusion
In this article, you learned how to change Users UPN automatically with scheduled task. Make sure that you place the ChangeUPN.ps1 PowerShell script in the C:\scripts\ folder. Create a new task in the task scheduler. Run the task and check that it did change users UPN. From now on, the Users UPN will change automatically.
Did you enjoy this article? You may also like Exchange 2016 OWA your connection is not secure. Don’t forget to follow us and share this article.
Handy Script,
Might I suggest going a step further? Why not use the event of account created and or account modified to automatically do this for you?