MCSMLab

View Original

Changing user logon domain in Office 365

 

When syncing your users to your Office 365 tenant via DirSync there are a number of reason that their login ID and primary SMTP address can end up being set to @tenant.onmicrosoft.com. Maybe you started DirSync before the domain was accepted in Office 365, or maybe your users UPNs are set to something other than the domain name you want to use as their primary SMTP address. Whatever the reason, once users are synced and end up with the wrong login ID, it can be a pain to change them especially for a large number of users. One way to fix that problem is with the following PowerShell command run after you connect to Azure AD via the Azure AD module.

Get-MsolUser | Where { -Not $_.UserPrincipalName.ToLower().StartsWith(“admin”) } | ForEach { Set-MsolUserPrincipalName -ObjectId $_.ObjectId -NewUserPrincipalName ($_.UserPrincipalName.Split(“@”)[0] + “@<domain>.com”) }

A couple of things to point out about what this command is doing...

Where { -Not $_.UserPrincipalName.ToLower().StartsWith(“admin”) }

The above section is filtering out any accounts that start with “admin”. This is there so that you don’t modify admin accounts that are created directly on the portal. You may need to change the “admin” to something else depending on your account naming standard.

ForEach { Set-MsolUserPrincipalName -ObjectId $_.ObjectId -NewUserPrincipalName ($_.UserPrincipalName.Split(“@”)[0] + “@<domain>.com”) }

This section does the actual work of changing the domain section of the user accounts. Obviously you will want to replace <domain>.com with your vanity domain name.

If you have any questions, let me know via a comment below or you can reach me via email.