Advertisement
Yevrag35

Change Email and UPN

Feb 9th, 2020
3,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $oldDom = '@newdomain.com'
  2. $newDom = '@olddomain.com'
  3.  
  4. # USING GET-ADUSER AS INPUT:
  5. # Get all AD users with an email address that ends in the old domain.  These will be the ones to change.
  6. $adFilter = 'mail -like "*{0}"' -f $oldDom
  7. $users = @(Get-ADUser -Filter $adFilter -Properties mail, proxyAddresses)
  8.  
  9. # Loop and process each found user
  10. foreach ($user in $users)
  11. {
  12.     # Locate old primary proxyAddress
  13.     $oldPrimary = $user.proxyAddresses.Where({$_.StartsWith('SMTP:')})
  14.  
  15.     if ($oldPrimary.Count -ne 1)    # That's weird...
  16.     {
  17.         Write-Warning ("{0} somehow either has multiple or no primary SMTP addresses?" -f $user.Name)
  18.         continue
  19.     }
  20.     else
  21.     {
  22.         $oldPrimary = $oldPrimary | Select-Object -First 1
  23.     }
  24.  
  25.     # Format the new 'mail' property for the given user.
  26.     $replacedEmail = $user.mail.Replace($oldDom, $newDom)
  27.  
  28.     # Use the replaced value to make the new primary proxyAddress.
  29.     $newPrimary = "SMTP:{0}" -f $replacedEmail
  30.  
  31.     # Format the new UPN
  32.     $newUpn = $user.UserPrincipalName.Replace($oldDom, $newDom)
  33.  
  34.     # Transform the 'old' primary from "SMTP:" to "smtp:".
  35.     $oldAlias = "{0}{1}" -f $oldPrimary.Substring(0, 4).ToLower(), $oldPrimary.Substring(4)
  36.  
  37.     # Announce
  38.     Write-Host ("Will replace '{0}' with '{1}'" -f $oldPrimary, $newPrimary) -f Yellow
  39.     Write-Host ("The old alias will be: {0}" -f $oldAlias) -f Yellow
  40.     Write-Host ("The new UPN will be: {0}" -f $newUpn) -f Yellow
  41.  
  42.     # Create new collection containing all current proxyAddresses
  43.     $addressCol = [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection]::new($user.proxyAddresses)
  44.  
  45.     # Find the index within the new collection of the old primary address.
  46.     $index = $addressCol.IndexOf($oldPrimary)
  47.  
  48.     # Replace that entry with the 'new' primary address.
  49.     $addressCol[$index] = $newPrimary
  50.  
  51.     # Then add the old primary as an additional proxyAddresses (alias)
  52.     [void] $addressCol.Add($oldAlias)
  53.  
  54.     # Build 'Set-ADUser' arguments
  55.     $setArguments = @{
  56.         Identity = $user.DistinguishedName
  57.         EmailAddress = $replacedEmail
  58.         Replace = @{ proxyAddresses = $addressCol.Value }
  59.         UserPrincipalName = $newUpn
  60.         WhatIf = $true      # Get rid of me to perform the changes
  61.     }
  62.  
  63.     # Execute
  64.     Set-ADUser @setArguments
  65.  
  66.     Write-Host "--------------------" -f Yellow     # Just a separator between each loop.
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement