With more and more small/medium businesses moving their email hosting to the cloud, we are finding that current UserPrincipalNames are either not usable in the cloud due to the domain name used or we would prefer not to.
After setting up a few clients, we here at Tech With a Hammer, decided there must be a better way for us lazy system administrators. Well, for those that are looking, here you can be just as lazy as us with the following PowerShell script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Import-Module ActiveDirectory $oSearchBase = "DC=techwithahammer,DC=local" $sNewUPN = "techwithahammer.com" try { Set-ADForest -UPNSuffixes @{Add="$sNewUPN"} } catch { Write-Host "Unable to add suffix, it is either already there or you are not running this script in an elevated prompt" } $oUsers = Get-ADUser -filter * -SearchBase $oSearchBase -Property EmailAddress,DisplayName | Where {$_.Enabled -eq $true -and $_.EmailAddress -ne $null -and $_.EmailAddress -ne ""} $oUsers | ForEach-Object { $oUser = $_ $sUPN = $oUser.UserPrincipalName Write-Host $oUser.DisplayName if ($sUPN -ne "" -and $sUPN -ne $null -and $sUPN.Split(@) -ge 2) { $sUPN = $sUPN.Replace($sUPN.Split('@')[ ($sUPN.Split('@').count - 1) ] ,$sNewUPN) } Try { Set-ADUser $oUser -UserPrincipalName $sUPN } Catch { # This hammer failed, but ignore all errors because we can } } |