Powershell command to fix UPN

I need to change the UPN for all users so I can migrate to office 365. I accidentally ran a powershell command on the entire domain instead of the test OU. Now all users UPN is just the @ symbol. When I go to Properties / Accounts, user logon name is correct but instead of the .local or .com domain, I just have the @ Symbol.

I can not figure out how to replace @ with their internet domain.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
A bit hard to help you without providing additional details, we cannot really guess what the domain name/UPN suffix you want to use is. But you can try something like this:

Get-ADUser -Filter * -SearchBase "DC=domain,DC=com" | % {Set-ADUser -Identity $_.ObjectGUID -UserPrincipalName "$($_.SamAccountName) + @domain.com" }

This will create new UPN value in the samaccountName@domain.com format. If you want to base the domain part on say the domain used for the primary SMTP address, use this:

Get-ADUser -Filter * -SearchBase "DC=domain,DC=com" -Properties proxyaddresses | % {Set-ADUser -Identity $_.ObjectGUID -UserPrincipalName ($($_.SamAccountName) + "@" + $(($_.proxyaddresses | ? {$_ -match "SMTP:*"}).Split("@")[1])) -WhatIf }

Open in new window

Goes without saying, TEST it first, against a single user, against 2-3 more user, before running against the whole domain.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ajdratchAuthor Commented:
Thanks so much. The first command fix it
FOXActive Directory/Exchange EngineerCommented:
Pretty Straightforward:

$oldSuffix = ""
$newSuffix = "yourdomain.com"
$server = "Yourdomaincontroller"
Get-ADUser -ResultSize Unlimited | ForEach-Object {
$newUpn = $_.UserPrincipalName.Replace($oldSuffix,$newSuffix)
$_ | Set-ADUser -server $server -UserPrincipalName $newUpn

ref link:  https://blogs.technet.microsoft.com/canitpro/2015/07/07/step-by-step-changing-the-upn-suffix-for-an-entire-domain-via-powershell/
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.