Updating Email addresses programmatically

Recently I was asked the question: ‘Can you remove a mailbox from email address policies to stop the reply address changing, but still have additional email addresses added automatically?’  The answer to this is no, not using the standard admin tools, but you could write a script to do this.  Below is the script I wrote to prove the concept.

The key elements are:

1.  Identifying which mailboxes are not controlled with email address policies.

$Addresses = @(get-mailbox `
      -filter {EmailAddressPolicyEnabled -eq $false})

2.  Extracting the current email addresses and appending an additional one. (Generate-Address is a function I wrote in the script that generates the email address in the format specified by the user running the script).

$old= @($Mailbox.emailaddresses | foreach `
      {$_.ProxyAddressString});
$new= Generate-Address $af1 $af2 $mailbox;
$old+=$new;

3.  Setting the new list of email addresses back to the mailbox.

Set-Mailbox $Mailbox -EmailAddresses $old -ea stop

The full code I used is below:


 

function Display-Menu
  {
  Write-host "There are $Num Mailboxes excluded from Email Address Policies"
  Write-Host "'n'nSelect From the Following Options"
  Write-Host "1. List Mailboxes excluded from Email Address Policies"
  Write-Host "2. Add an address to Mailboxes excluded from Email Address Policies"
  Write-Host "3. Quit"
  $inp = read-host
  return $inp
  }
 
function Update-Address ($Mailboxes)
  {
  $AF1 = Get-AddressFormat1
  Do
    {
    $AF2 = Get-AddressFormat2
    }
  Until (1..4 -contains $AF2)
  ForEach ($Mailbox in $Mailboxes)
    {
    $old = @($Mailbox.emailaddresses | foreach {$_.ProxyAddressString});
    $new = Generate-Address $af1 $af2 $mailbox;
    $old += $new;
    try
      {
      Set-Mailbox $Mailbox -EmailAddresses $old -ea stop
      }
    catch
      { 
      write-host ($mailbox.displayname + " already has the address "+ $new.substring(5)) -fore red
      }
    }
  }
 
function Get-AddressFormat1 
  {
  Write-host "Enter the SMTP domain name you wish to add eg QA.COM"
  $inp = read-host
  return $inp
  }
 
function Get-AddressFormat2 
  {
  write-host "Select the format to be used for generating the address:"
  write-host "1. Use Alias"
  write-host "2. First name.Last name (john.smith)"
  write-host "3. First name initial and Last name (jsmith)"
  write-host "4. First name and Last name initial (johns)"
  $inp = read-host
  return $inp
  }
 
function Generate-Address ($Ad1 , $Ad2 , $MB) 
  {
  switch ($Ad2)
    {
    1 { $addy = "smtp:" + $mb.alias + "@" + $ad1 ;  break}
    2 { $addy = "smtp:" + $mb.displayname.split()[0] + "." + $mb.displayname.split()[1] + "@" + $ad1; break}
    3 { $addy = "smtp:" + $mb.displayname.split()[0].substring(0,1) + $mb.displayname.split()[1]  + "@" +$ad1; break}
    4 { $addy = "smtp:" + $mb.displayname.split()[0]  + $mb.displayname.split()[1].substring(0,1) + "@" + $ad1; break}
    }
  return $addy
  }
 
Write-host "Gethering data..."
$Addresses = @(get-mailbox -filter {EmailAddressPolicyEnabled -eq $false})
$Num = $Addresses.count
Do
  {
  Do
    {
    cls
    $var = Display-Menu
    }
  until (1..3 -contains $var)
  Switch ($var)
    {
    1{ $Addresses | FT Name , EmailAddresses -a; read-host "Press enter key to continue"; break}
    2{ Update-Address -Mailboxes $Addresses; read-host "Press enter key to continue"; break}
    3{ break}
    }
  }
  until ($var -eq 3)