Link to home
Start Free TrialLog in
Avatar of Tatankasa
Tatankasa

asked on

Exchange 2013 Powershell script to manually add new email addresses for new domain based on old ones

Hey all!  So my Google powers have let me down today.  I'm preparing to add a new domain to our Exchange server, and I need to ensure that all email addresses that exist for our old domain are duplicated with the new domain for all our mailboxes.  I believe I could do this fairly well using email address policy (just a simple all users get <alias>@<newdomain>) but I know that a lot of our mailboxes have one or two aliases on the old domain, and a bunch of them are set to not follow the policy anyways.

So, what I'm hoping to find/build is a script that crawls through each mailbox in turn, pulls all the email addresses, and for each address that exists on the old domain, create an additional one on the new domain.  The other hitch is that we have two other domains, each used by a handful of mailboxes, that are not to be affected by this, so it would not be enough to simply duplicate every address, it would have to check each address to ensure it was @<olddomin>, and not @<otherdomain1> or @<otherdomain2>.

I'll also have to do something similar with setting the default reply address for each mailbox.  Read the default smtp reply address and if it's @<olddomain>, set the @<newdomain> address as the default smtp reply.

Anyone know of such a script?  Or possibly you are such a powershell master you could whip it up with little effort?  I can probably do a lot of it myself if I have something to work from, but I'm not so good with a lot of the specific syntax related to pulling attributes like email addresses and parsing them for comparisons.  Any assistance would be appreciated, thanks!
ASKER CERTIFIED SOLUTION
Avatar of Akhater
Akhater
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tatankasa
Tatankasa

ASKER

That was EXACTLY what I was looking for, thanks!!  Ya, I figured it would be simple for someone better than I at powershell, but alas I still struggle unless I have some code to start from/reference.  I had trouble with the PrefixString bit, so I changed it to a sloppier Split(":")[0] but it works.  I also added some additional sloppy code to reduce the primary SMTP prefix to smtp so the new address won't conflict with the current primary.  I intend to modify the default email policy to change the primary for all those mailboxes still using the policy, and then I added a catch to the script to manually change it for those mailboxes that aren't using the policy.  Thanks again!!!!

[code]$mbxs = Get-Mailbox -ResultSize unlimited
 
$olddomain = "<olddomain>"
$newdomain = "<newdomain>"
 
foreach ($mbx in $mbxs){
 
$SMTPAddresses = $mbx.EmailAddresses | where {$_.Split(":")[0] -eq "SMTP"}
 
    foreach($SMTPAddress in $SMTPAddresses){
          $Addressdomain = $SMTPAddress.Split("@")[1]
          $AddressAlias  = $SMTPAddress.Split("@")[0]
        Write-Host ""
          Write-Host $AddressAlias " " $Addressdomain
          if($Addressdomain -eq $olddomain){
            if($SMTPAddress.Split(":")[0] -ceq "SMTP"){
                $AddressAlias = "smtp:" + $AddressAlias.Split(":")[1]
                $NewSMTPAddress = "$AddressAlias@$newdomain"
                Write-Host "Yes old domain so new address:" $NewSMTPAddress
                #Set-Mailbox $mbx.Alias -EmailAddresses @{add=$NewSMTPAddress}
                if(!($mbx.EmailAddressPolicyEnabled)){
                    Write-Host "Policy not enabled, so manually make primary"
                    #Set-Mailbox $mbx.Alias -PrimarySmtpAddress ($NewSMTPAddress.Split(":")[1])
                }
            }
            else {
                $NewSMTPAddress = "$AddressAlias@$newdomain"
                    Write-Host "Yes old domain so new address:" $NewSMTPAddress
                    #Set-Mailbox $mbx.Alias -EmailAddresses @{add=$NewSMTPAddress}
            }
        }
        else {
            Write-Host "Not old domain, so no new address"
        }
    }
}
[/code]
you are most welcome, glad  was able to help

thanks for the updated version too :D