Managing shared mailboxes in Exchange Online can become repetitive when dealing with large environments or migration projects. Instead of creating each mailbox manually through the Exchange admin centre, we can automate the process with a simple PowerShell script. This not only saves time but also reduces the chance of mistakes.

In this article, we’ll walk through a script that reads mailbox information from a CSV file and automatically creates shared mailboxes with the right display names, aliases and email addresses.


Why Automate Shared Mailbox Creation?

Shared mailboxes are widely used in organisations for generic email addresses like hr@company.com or marketing@company.com . When setting up multiple mailboxes, doing it one by one is slow and prone to human error. Automating the process with PowerShell ensures consistency, accuracy and efficiency.


Preparing the CSV File

Before running the script, prepare a CSV file with the required mailbox information. For example:

DisplayName,Alias,PrimarySMTPAddress,SecondSmtp
Support Mailbox,support,support@company.com,support.alt@company.com
HR Enquiries,hr,hr@company.com,hr.alt@company.com
  • DisplayName – The friendly name shown in Outlook/GAL.
  • Alias – The unique mailbox alias.
  • PrimarySMTPAddress – The main email address for the mailbox.
  • SecondSmtp – (Optional) an additional smtp address.

Save this file as C:\Temp\SharedMailboxes.csv.


The PowerShell Script Explained

Here’s a breakdown of the key parts of the script:

1. Importing the CSV

$sharedMailboxes = Import-Csv "C:\Temp\SharedMailboxes.csv"

This reads the CSV file into PowerShell so each row can be processed as a mailbox object.


2. Looping Through Each Mailbox

foreach ($mailbox in $sharedMailboxes) {
    $displayName   = $mailbox."DisplayName"
    $alias         = $mailbox."Alias"
    $smtpAddress   = $mailbox."PrimarySMTPAddress"
    $secondarySmtp = $mailbox."SecondSmtp"

Here we extract the values from each row of the CSV into variables for use later in the script.


3. Checking if the Mailbox Already Exists

$existingMailbox = Get-Mailbox -Identity $alias -ErrorAction SilentlyContinue

if ($existingMailbox) {
    Write-Host "Shared Mailbox $displayName ($smtpAddress) already exists. Skipping..."
}

This prevents the script from trying to recreate mailboxes that are already present.


4. Creating the Shared Mailbox

New-Mailbox -Shared -Name $displayName -DisplayName $displayName -Alias $alias -PrimarySmtpAddress $smtpAddress

If the mailbox doesn’t exist, it is created with the details provided. If no primary SMTP is given, Exchange will generate one automatically.


5. Adding a Secondary SMTP

if ($secondarySmtp -and $secondarySmtp.Trim() -ne "") {
    Set-Mailbox -Identity $alias -EmailAddresses @{add=$secondarySmtp}
    Write-Host "Added secondary SMTP: $secondarySmtp to $displayName"
}

This ensures additional addresses are added where required.


Example in Practice

Imagine your HR department needs three new shared mailboxes for recruitment, employee relations and payroll. By simply adding three rows to the CSV file and running the script, all three mailboxes are created in seconds with the correct configuration. No manual clicking, no errors and consistent naming.


Conclusion

Automating shared mailbox creation with PowerShell is a simple but powerful way to streamline Exchange Online administration. By combining a well-structured CSV with a reusable script, IT teams can quickly deploy mailboxes at scale with minimal effort.



Full PowerShell Script

Below is the complete script that ties everything together. Update it as needed, then run it in an Exchange Online PowerShell session.

# Import shared mailboxes from CSV
$sharedMailboxes = Import-Csv "C:\Temp\SharedMailboxes.csv"

foreach ($mailbox in $sharedMailboxes) {
    $displayName   = $mailbox."DisplayName"
    $alias         = $mailbox."Alias"
    $smtpAddress   = $mailbox."PrimarySMTPAddress"
    $secondarySmtp = $mailbox."SecondSmtp"

    try {
        # Check if mailbox already exists
        $existingMailbox = Get-Mailbox -Identity $alias -ErrorAction SilentlyContinue

        if ($existingMailbox) {
            Write-Host "Shared Mailbox $displayName ($smtpAddress) already exists. Skipping..."
        }
        else {
            # Create the shared mailbox
            if ($smtpAddress -and $smtpAddress.Trim() -ne "") {
                New-Mailbox -Shared -Name $displayName -DisplayName $displayName -Alias $alias -PrimarySmtpAddress $smtpAddress
                Write-Host "Created Shared Mailbox: $displayName ($smtpAddress)"
            }
            else {
                New-Mailbox -Shared -Name $displayName -DisplayName $displayName -Alias $alias
                Write-Host "Created Shared Mailbox: $displayName (Exchange will assign default SMTP)"
            }

            # Add secondary SMTP if provided
            if ($secondarySmtp -and $secondarySmtp.Trim() -ne "") {
                Set-Mailbox -Identity $alias -EmailAddresses @{add=$secondarySmtp}
                Write-Host "Added secondary SMTP: $secondarySmtp to $displayName"
            }
        }
    }
    catch {
        Write-Warning "Failed to create shared mailbox $displayName ($smtpAddress): $_"
    }
}