Jump to content

Recommended Posts

Posted
having run local exchange for years and having the email address format different to the username, now that I have moved fully to 365. how can i automate the local UPN to change to the same format forename.surname@ as all other users when it syncs to 365 via azure AD connect?
Posted

Assuming the Email Address field is set, and you want the email address to become the UPN, then a simply powershell script will do this.

 

I have one somewhere but ChatGPT came up with this

 

# Import Active Directory module

Import-Module ActiveDirectory

 

# Get all users from Active Directory

$users = Get-ADUser -Filter *

 

# Loop through each user and set UPN to match email attribute

foreach ($user in $users) {

$email = $user.EmailAddress

$currentUPN = $user.UserPrincipalName

$domain = "yourdomain.com" # Change this to your domain

 

if ($email -ne $null) {

$newUPN = $email

if ($currentUPN -ne $newUPN) {

Set-ADUser -Identity $user -UserPrincipalName $newUPN -Server $env:USERDNSDOMAIN

Write-Host "Changed UPN for $($user.Name) to $newUPN"

} else {

Write-Host "UPN for $($user.Name) is already set to $newUPN"

}

}

else {

Write-Host "No email found for $($user.Name)"

}

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...