Skip to content

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.

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.

Change Users UPN automatically with scheduled task script in folder

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.

Change Users UPN automatically with scheduled task 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.

  1. We start with the first tab General.
  2. Name: ChangeUPN.
  3. 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.
  4. An account with privileges for running the task.
  5. Click on Run whether user is logged on or not.
  6. Enable checkbox Run with highest privileges.
  7. 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.
Change Users UPN automatically with scheduled task general tab

Click the tab Triggers. Click New…

Change Users UPN automatically with scheduled task triggers tab

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.

Change Users UPN automatically with scheduled task new trigger

Click the tab Actions. Click New…

Change Users UPN automatically with scheduled task actions tab

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.

Change Users UPN automatically with scheduled task new action

Click the tab Settings. Change the option stop the task if it runs longer than to 4 hours. Click OK.

Change Users UPN automatically with scheduled task settings tab

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.

Change Users UPN automatically with scheduled task before

Click the created task UsersUPN and click Run. The Last Run Result will show that The operation completed successfully. (0x0).

Change Users UPN automatically with scheduled task run task

After running the change Users UPN task

Refresh or restart ADUC and go to the user. Verify that the UPN suffix is successfully changed.

Change Users UPN automatically with scheduled task after

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 Server OWA your connection is not secure. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has One Comment

  1. 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?

Leave a Reply

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