Advertisement
SethAluma

ADSetEmailAliases

Aug 14th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Server     = 'adserver.local.lan' # <-- Enter your AD server FQDN name here
  2. $OldDomain  = '@olddomain.com'     # <-- Enter your old domain here
  3. $NewDomain  = '@newdomain.com'     # <-- Enter your new domain here
  4. $SearchBase = 'DC=local,DC=lan'    # <-- Enter your LDAP Domain here (all user accounts from this OU and sub-OUs will have emails set)
  5. $Credential = Get-Credential       # <-- This prompts for your AD server login details
  6.  
  7. $Accounts   = Get-ADUser -Server $Server -Credential $Credential -Filter * -SearchBase $SearchBase -Properties UserPrincipalName, SamAccountName, EmailAddress, ProxyAddresses, MsExchIMAddress, Name | Sort-Object Name
  8.  
  9. foreach ($Account in $Accounts) {
  10.     $Account.ProxyAddresses = $Account.ProxyAddresses | % { $_.ToLower() }
  11.     $OldEmailAddress        = ($Account.SamAccountName + $OldDomain).ToLower()
  12.     $NewEmailAddress        = ($Account.SamAccountName + $NewDomain).ToLower()
  13.     $OldEmailSMTP           = "smtp:" + $OldEmailAddress
  14.     $NewEmailSMTP           = "SMTP:" + $NewEmailAddress
  15.  
  16.     # Set AD "proxyAddress" attribute
  17.     if (-not $Account.ProxyAddresses.Contains($OldEmailSMTP)) {
  18.         $Account.ProxyAddresses.Add($OldEmailSMTP) | Out-Null
  19.     } else {
  20.         $Account.ProxyAddresses[[array]::IndexOf($Account.ProxyAddresses, $OldEmailSMTP)] = $OldEmailSMTP
  21.     }
  22.  
  23.     if (-not $Account.ProxyAddresses.Contains($NewEmailSMTP)) {
  24.         $Account.ProxyAddresses.Add($NewEmailSMTP) | Out-Null
  25.     } else {
  26.         $Account.ProxyAddresses[[array]::IndexOf($Account.ProxyAddresses, $NewEmailSMTP)] = $NewEmailSMTP
  27.     }
  28.  
  29.     # Set other attributes
  30.     $Account.UserPrincipalName = $OldEmailAddress # <-- This is the email you want users to use as their O365 usernames
  31.     $Account.Mail              = $OldEmailAddress # <-- This is the email you want in your user's "mail" AD field
  32.     $Account.MsExchIMAddress   = $OldEmailAddress # <-- This is the email you want to use for "Skype for Business"
  33.  
  34.     # Save changes to AD
  35.     Set-ADUser -Server $Server -Credential $Credential -Instance $Account
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement