Appending SMTP aliases to multiple mailboxes in O365 can be a pain, thankfully PowerShell allows us to do this is bulk and save tons of time.


Want to say thanks for an ad-free, subscription free experience and top-notch content?

Buy me a coffeeBuy me a coffee

To bulk add SMTP aliases to users, you'll first want to create a spreadsheet with two headers. In this example, we'll call them User and Alias. Add the mailbox UPNs to the User column and the desired aliases to the Alias column. Note: multiple aliases need to be comma separated as demonstrated in line 3 of our example CSV template below.

Once you've got your template filled out, save it as a .CSV and take note of the location of the file because you're going to set it as your current directory momentarily. In this example, I'm going to save the file at C:\Users\ExampleUser\Documents and name the file apal.csv

First, let's get a remote PowerShell session started by running the following commands:

Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy Unrestricted
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session 
Connect-MsolService
Import-Module Msonline

Now that we've got a powershell session going, let's CD to the directory that our CSV file lives in:

cd "C:\Users\ExampleUser\Documents"

To avoid anything awful happening, let's check to make sure we're in the right location using the ls command:

ls

You should see feedback that looks something like this after running the command:

PS C:\Users\ExampleUser\Documents> ls

    Directory: C:\Users\ExampleUser\Documents
    
Mode                LastWriteTime         Length Name
----                -------------         ------ ----     
-a----        1/24/2020  11:51 PM           3973 apal.csv 

Now that I have confirmed I am in the correct directory and can see the file, it's time to append the SMTP aliases.

$users = import-csv '.\apal.csv'
foreach ($user in $users)
{
    Set-Mailbox $user.upn -EmailAddresses @{add=$user.alias}
 
    Write-Host "$($user.UPN) now has the alias of ($user.alias)"
}
$users = $null

Assuming everything was done correctly, you should get some feedback that looks like this:

[email protected] now forwards to (@{[email protected]; [email protected]}.alias)

That's it, you're done! Rinse and repeat if necessary.

Share this post