Skip to content

Change alias address to primary SMTP address with PowerShell

You already have the primary SMTP address and the alias address for each mailbox set. However, the task is to swap the alias address with the primary SMTP address. The best way to do this is with PowerShell. In this article, you will learn how to change the alias address to the primary SMTP address with PowerShell.

Introduction

The Set-PrimaryAddress.ps1 PowerShell script works for:

  • Exchange on-premises
  • Exchange Hybrid*
  • Exchange Online (coming soon)

*For Exchange Hybrid you need to change the Get-Mailbox to Get-RemoteMailbox and Set-Mailbox to Set-RemoteMailbox in the script.

Note: The alias smtp address becomes the primary SMTP address, and the primary SMTP address will become an alias address. So the primary SMTP address will not get removed, and it just gets swapped with the alias smtp address.

Before you start

What if there is no alias address already set for every mailbox? The best is to go through the article Bulk add secondary SMTP address with PowerShell.

Once you have verified that the alias address is set for all the mailboxes, run the Set-PrimaryAddress.ps1 PowerShell script from this article to swap the alias address with the primary SMTP address.

Suppose you are done and want to remove the old primary SMTP address that is now an alias address, you can go through the article Bulk remove secondary SMTP address with PowerShell.

It’s always better to do this in stages than with one script that does it all immediately so you are sure everything is correctly set.

Prepare set Primary SMTP address PowerShell script

Download the Set-PrimaryAddress.ps1 PowerShell script or copy and paste the code below into Notepad. Give it the name Set-PrimaryAddress.ps1 and place it in the C:\scripts folder. Create a scripts folder if you don’t have one.

# Output will be added to C:\temp folder. Open the log with a text editor.
Start-Transcript -Path C:\temp\Set-Primary-SMTP-Address.log -Append

# Specify the domain to set as primary SMTP
$primarySMTPDomain = "@exoip.nl"

# Get all mailbox users
$users = Get-Mailbox -ResultSize Unlimited

foreach ($user in $users) {
    $currentPrimarySMTP = $user.PrimarySmtpAddress.ToString()
    $aliasAddresses = $user.EmailAddresses | Where-Object { $_ -clike "smtp*" -and $_ -clike "*$primarySMTPDomain" }

    # Check if the current primary SMTP address ends with the specified domain
    if ($currentPrimarySMTP -like "*$primarySMTPDomain") {
        Write-Host "Skipping $($user.DisplayName) - Primary SMTP already ends with $primarySMTPDomain" -ForegroundColor Yellow
    }
    elseif ($aliasAddresses.Count -eq 1) {
        $newPrimarySMTP = $aliasAddresses[0] -replace "smtp:", ""
        Write-Host "Updating primary SMTP for $($user.DisplayName) to $newPrimarySMTP" -ForegroundColor Green

        # Set the new primary SMTP address
        Set-Mailbox -Identity $user -PrimarySmtpAddress $newPrimarySMTP -EmailAddressPolicyEnabled $false -WhatIf
    }
    elseif ($aliasAddresses.Count -eq 0) {
        Write-Host "No alias address found for $($user.DisplayName) - Primary SMTP not updated" -ForegroundColor Cyan
    }
    else {
        Write-Host "Multiple alias addresses found for $($user.DisplayName) - Primary SMTP not updated" -ForegroundColor Red
    }
}

Stop-Transcript
  • Line 5: Change the @exoip.nl value to the domain you want it to look for in the alias smtp addresses.

Bulk set Primary SMTP address PowerShell script

The script works for Exchange on-premises, Exchange Hybrid, and Exchange Online. You must connect with the proper tools before you run the script:

Suppose you want to target the cloud mailboxes in an Exchange Hybrid environment, you need to change two cmdlets in the script. Change the Get-Mailbox to Get-RemoteMailbox to retrieve all the cloud mailboxes and change the Set-Mailbox to Set-RemoteMailbox to apply the changes to the cloud mailboxes.

Go to the script path and run the Set-PrimaryAddress.ps1 script. The script will go through all the mailboxes in the Exchange Organization.

C:\scripts\.\Set-PrimaryAddress.ps1 

Note: On line 23, there is a -WhatIf parameter for a dry run so that nothing will happen, and you can double-check in the PowerShell output if everything looks like you want. Once everything looks good, remove the What-If parameter and rerun the script.

If multiple alias addresses for a mailbox exist, it will not apply any changes, and the output will show that information so you can look into it.

In my example, the existing alias address with the domain @exoip.nl will be set in bulk as the primary SMTP address.

Transcript started, output file is C:\temp\Set-Primary-SMTP-Address.log
Multiple alias addresses found for Alysia Maverick - Primary SMTP not updated
No alias address found for Boris Campbell - Primary SMTP not updated
Updating primary SMTP for  Christopher Payne to Christopher.Payne@exoip.nl
No alias address found for Discovery Search Mailbox - Primary SMTP not updated
Updating primary SMTP for  James Paterson to James.Paterson@exoip.nl
No alias address found for Fraser, Max - Primary SMTP not updated
Updating primary SMTP for Nicholas Murray to Nicholas.Murray@exoip.nl
No alias address found for Richard Hunter - Primary SMTP not updated
No alias address found for sharedmailboxonprem - Primary SMTP not updated
Transcript stopped, output file is C:\temp\Set-Primary-SMTP-Address.log

The existing smtp alias address is now the primary SMTP address for all the users.

Read more: Send from Alias in Office 365 »

Conclusion

You learned how to bulk set alias address as primary SMTP address with PowerShell. First, download the Set-PrimaryAddress PowerShell script. Then, add the domain in the script you want it to search for in the mailbox alias addresses. At last, run the script. Remember to test first with the -WhatIf parameter.

Did you enjoy this article? You may also like Create shared mailbox with same alias. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *