Need to swap the primary domain and alias domain email address on Office 365

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Nick Hawkins Nick Hawkins 7 months, 3 weeks ago.

  • Author
    Posts
  • #35130
    Profile photo of Robert Gardner
    Robert Gardner
    Participant

    I have a client that I have partner admin login for Office 365 online. They have added a new domain and have created alias email addresses with the new domain. Now they want to make the alias email address the primary and the primary the alias. I have pieced together a script but am concerned that it will actually add the new domain in a second time. Please look at the script and help me make this thing work right (correct) the first time. Thanks.

    # Supply User credentials with admin privileges
    $cred = Get-Credential
    # Initial connection to Partner Exchange Online
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/PowerShell-LiveID?DelegatedOrg=(currentprimarydomain).onmicrosoft.com -Credential $cred -Authentication Basic –AllowRedirection
    # Final connection to Exchange Online
    Import-PSSession $Session
    # Define the new primary domain
    $NewEmailDomain = '@(currentaliasdomain)'
    # Get a list of mailboxes and store in a variable
    $Mailboxes = Get-Mailbox -ResultSize Unlimited
    # Change the primary email address for each mailbox to the new domain
    foreach ($Mailbox in $Mailboxes)
    {
    $Address = "smtp:$($Mailbox.Alias)$NewEmailDomain"
    $Mailbox.EmailAddresses.Add($Address)
    Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses $Mailbox.EmailAddresses -WindowsEmailAddress $Address
    }
    # Close Session when finished
    Remove-PSSession $Session

  • #37473
    Profile photo of Nick Hawkins
    Nick Hawkins
    Participant

    You could add some simple if else statements to evaluate something before you take action on it. In the context of what you are doing, here is an example of that:

    # Check if new email address already exists on mailbox
    if ($mailbox.EmailAddresses -contains $Address) {
        Write-Warning "Maibox: $($mailbox.Name) already has email address: $Address"
    }
    
    # If the email does not already exist on the mailbox, add it
    else {
        Write-Warning "Mailbox: $($mailbox.Name) does not have email address: $Address -- Script will add $Address"
        # Add the powershell here to add the email address to the mailbox
    }
    

    You could use similar logic on the additional stuff you need to flip to make the new email alias primary.

    Note: Nothing above has been tested, I am just showing you some example logic.

    Some other notes that are not so PowerShell related:

    This script is assuming that you are working with cloud only accounts, if these accounts are being synced up to Office 365 via Azure AD Connect (DirSync) you will need to make these changes in Active Directory and have them sync up to the cloud.

    Be careful grabbing all of the mailboxes like your example and making a change like this, you may want to include filtering out of the discovery search mailbox or do something where you grab all users that have a specific license on them instead.

    -Nick

You must be logged in to reply to this topic.