In the current article series, we will review the different options for implementing Automatic Reply…
Bulk create shared mailboxes with PowerShell
To add a shared mailbox in Exchange Online, you can do this manually in Microsoft 365 Admin Center. But it’s much faster to create multiple shared mailboxes and add users with access rights with a PowerShell script. In this article, you will learn how to bulk shared mailboxes in Exchange Online with PowerShell.
Table of contents
Connect to Exchange Online
Before creating shared mailboxes in the next step, you must install and connect to Exchange Online PowerShell. Start Windows PowerShell as administrator and run the below cmdlet.
Connect-ExchangeOnline
Create bulk shared mailboxes with PowerShell
For this step, you must create a single CSV file to use in PowerShell as a source file. Initially, we want to keep it simple and start to create bulk shared mailboxes only.
Create a single CSV file
First, we need to type the data into notepad and save it as a CSV file. In our example, it is saved as info.csv in C:\temp.
Note: The name of the shared mailbox must be unique.
You should use the following format for the CSV file:
- Name: Unique email address of the shared mailbox
- DisplayName: Name of the shared mailbox
Check CSV file with Import-Csv cmdlet
Run Windows PowerShell as administrator. Make sure that PowerShell can read the file, run Import-Csv cmdlet.
Import-Csv "C:\temp\info.csv"
This is how it looks in our example.
PS C:\> Import-Csv "C:\temp\info.csv"
Name DisplayName
---- -----------
Finance1 Finance Team
Marketing1 Marketing Team
HR1 HR Department
IT1 IT Department
Security1 Security Department
Management1 Management Team
Design1 Design Team
Sales1 Sales Team
Production1 Product Team
RD1 Research and Development
Run PowerShell script to create bulk shared mailboxes in Exchange Online
Use the below PowerShell script to create new shared mailboxes in Exchange Online.
# Import CSV file
$Datas = Import-Csv "C:\temp\info.csv"
# Get all recipients
$Recipients = Get-Recipient -ResultSize Unlimited | select Name
foreach ($Data in $Datas) {
# Check if shared mailbox does not exist
If (($Recipients | Where { $_.Name -eq $Data.Name }) -eq $Null) {
# Create shared mailbox
New-Mailbox -Name $Data.Name -DisplayName $Data.DisplayName -Shared
Write-Host -f Green "Shared mailbox '$($Data.Name)' created successfully."
}
Else {
Write-host -f Green "Shared Mailbox '$($Data.Name)' already exists."
}
}
Check shared mailboxes in Microsoft 365 Admin Center
You managed to create bulk shared mailboxes in PowerShell. You can also check which shared mailboxes you created in the Microsoft 365 admin center.
The outcome will look like the picture below.
We have not assigned any users to these shared mailboxes. Look into the next step to add users with full access rights to each of these shared mailboxes.
Create bulk shared mailboxes with users access rights in PowerShell
For this step, you must create a CSV file with more data to use as a source file in PowerShell. We want to create shared mailboxes, import multiple users, and assign permission.
Create CSV to create bulk shared mailboxes with users
You need to type the following data format in the CSV file:
- Name: Unique email of the shared mailbox
- DisplayName: Name of the shared mailbox
- User: Active users
- AccessRights: FullAccess
In our example, we typed the data into notepad and named the file info.csv and placed it in C:\temp.
Check CSV file with Import-Csv cmdlet
Run Windows PowerShell as administrator. Make sure that PowerShell can read the file, run Import-Csv cmdlet.
Import-Csv "C:\temp\info.csv"
This is how it looks in our example.
PS C:\> Import-Csv "C:\temp\info.csv"
Name DisplayName User AccessRights
---- ----------- ---- ------------
Finance1 Finance Team Amanda Morgan;Stephen Hunter;Carol Baker FullAccess
Marketing1 Marketing Team Craig Hansen;Edward Jones FullAccess
HR1 HR Department Edward Wilson;Emma Arnold;Nick Carlson;Lisa Philips FullAccess
IT1 IT Department Piers Bower;Lisa Davies;Anna Welch;John Hamod FullAccess
Security1 Security Department Edward Wilson;Jonathan Fisher FullAccess
Management1 Management Team Neil Graham;Sarah Coleman;Viola Simon FullAccess
Design1 Design Team Wallace Zheng;Abigail Hodges;Emma Arnold FullAccess
Sales1 Sales Team Edward Lincoln;Amanda Morgan;Stephen Hunter FullAccess
Production1 Product Team Kylie Davidson;Mary Walsh FullAccess
RD1 Research and Development Max Gibson;Curt Berry;Owen Simpson FullAccess
Run PowerShell script to bulk create shared mailboxes in Exchange Online with users access rights
Run the below PowerShell script to create shared mailboxes in Exchange Online. You can create multiple shared mailboxes, add existing users and assign permission simultaneously.
The PowerShell output lets you know if the shared mailbox already exists. If you have an incorrect user in your CSV file, the output will inform you that the user was not found. It will also not add an existing user twice to the same shared mailbox. You will get a warning that the user access rights are already present on this shared mailbox.
# Import CSV file
$Datas = Import-Csv "C:\temp\info.csv"
# Get all recipients
$Recipients = Get-Recipient -ResultSize Unlimited | select Name
foreach ($Data in $Datas) {
# Check if shared mailbox does not exist
If (($Recipients | Where { $_.Name -eq $Data.Name }) -eq $Null) {
# Create shared mailbox
New-Mailbox -Name $Data.Name -DisplayName $Data.DisplayName -Shared
Write-Host -f Green "Shared mailbox '$($Data.Name)' created successfully."
}
Else {
Write-Host -f Green "Shared Mailbox '$($Data.Name)' already exists."
}
# Assign permissions on shared mailbox
$Users = $Data.User -split ";"
foreach ($User in $Users) {
Add-MailboxPermission -Identity $Data.Name -User $User.Trim() -AccessRights $Data.AccessRights
}
}
Check shared mailboxes with its users and permission in Microsoft 365 Admin Center
You will see the shared mailboxes in the Microsoft 365 admin center. If you click on each shared mailbox, you will see how many users are assigned and their permission.
If you click Read and manage permissions, you will see which users are assigned to the shared mailbox.
Did you find it easy to use a PowerShell script to create bulk shared mailboxes with users and grant them access rights?
Read more: Enable reply all storm protection in Exchange Online »
Conclusion
You learned how to create bulk shared mailboxes with a PowerShell script and a CSV file. It is the fastest way to import over a thousand users and assign their access rights to multiple shared mailboxes. Our last PowerShell script allows you to create bulk shared mailboxes, add multiple users to each shared mailbox and grant them access rights.
Did you enjoy this article? You may also like Save sent items in shared mailbox. Don’t forget to follow us and share this article.
If I were to create bulk shared mailboxes with users access rights “FullAccess” and “SendAs” in PowerShell, will it be possible ? And how would the csv file and script look like ?