Lets take a look at how we can create a shared mailbox in Office 365 Exchange online using PowerShell. Using PowerShell will help you reduce administrative overhead, removes the need to login to the slow and cumbersome Microsoft 365 admin center and helps you create automated tasks .
In this post we are going to cover how you can create a shared mailbox and add member to a shared mailbox using PowerShell as well as provide example scripts.
If you are looking on how to do this from within the admin center, click here: how to create a shared mailbox using the Microsoft 365 Admin Center.
What is a shared mailbox?
A shared mailbox is mailbox that multiple people can access and send mail from. They are often also used to provide a common calendar where multiple staff can access it, such as holiday time or work scheduling.
Often, shared mailboxes will have a generic name such as; [email protected] or [email protected]. This allows for centralised services to be provided for a team of staff.
Shared mailboxes can only be accessed with delegated access from a licensed (paid for) account.
Do Microsoft 365 Shared Mailboxes cost?
Shared mailboxes in Office 365 are free. They do not have a username or password to access them, but must be accessed via delegation from a licensed (paid for) user account.
Create a Shared Mailbox with PowerShell
1. Start by connecting to Exchange Online PowerShell. If you are unsure how to do this, follow my guide here: How To Connect to Exchange Online with PowerShell
2. Use the New-Mailbox cmdlet to create a new shared mailbox.
In the below example I create a mailbox with the name of Support Department, Display Name of Support Department and the primary email address of [email protected].
New-Mailbox -Shared -Name "Support Department" -DisplayName "Support"`
-Alias support
3. To view full details of your mailbox and learn what parameters you can modify, use the Get-Mailbox cmdlet and reference the Microsoft docs for parameter types: here.
Get-Mailbox -identity support | fl
You can also verify this completed successfully by logging into the admin portal at admin.microsoft.com and then open Teams & Groups > Shared mailboxes.
Add and remove users from a Shared Mailbox with PowerShell
Now you have created your mailbox, lets look at how we can add and remove users to a shared mailbox using PowerShell.
Here is an example script that adds a users to our support mailbox. For this we use the Add-MailboxPermission cmdlet.
Add-MailboxPermission -Identity support -AccessRights FullAccess `
-InheritanceType All -AutoMapping:$true -User [email protected]
Now to remove a user from our shared mailbox, we need to use the Remove-MailboxPermission cmdlet:
Remove-MailboxPermission -Identity support `
-AccessRights FullAccess -Confirm:$false `
-User [email protected]
If you want to add a single user to multiple mailboxes we can use a similar script to the below with the Get-Mailbox cmdlet:
Get-Mailbox -ResultSize unlimited -Filter `
"(RecipientTypeDetails -eq 'sharedMailbox') -and (Alias -ne 'admin')" | `
Add-MailboxPermission -User [email protected] -AccessRights FullAccess `
-InheritanceType All
Also if you are looking to add multiple users to a single mailbox, you can create a variable that stores your users list and loop through each user, as follows:
$users = "[email protected]","[email protected]" ,"[email protected]"
foreach ($user in $users) {
Add-MailboxPermission -Identity support -AccessRights FullAccess `
-InheritanceType All -AutoMapping:$true -User $user
}
View Shared Mailbox permissions with PowerShell
You can view the permissions of a shared mailbox using the Get-MailboxPermission cmdlet like in the example below:
Get-MailboxPermission -Identity support | `
Where-Object {($_.User -Notlike "NT AUTHORITY\SELF")}
In our example, we have also used the Where-Object cmdlet to remove any bloat in the results so they are relevant.
Without the Where-Object cmdlet, our results might look like the following:
And when using Where-Object to filter our results you can see we only see the relevant information on the screen:
Delete a Shared Mailbox with PowerShell
To delete a shared mailbox with PowerShell, we can use the Remove-Mailbox cmdlet. If this command was to be run on a user mailbox, it would delete both the mailbox and the associated user account, so be mindful when using this cmdlet.
Here is an example of me deleting the ‘support’ shared mailbox:
Remove-Mailbox -identity support
Once you run the above script, you will be prompted to confirm the action of deleting the mailbox. I suggest you leave this as a manual process, as you can double check you want to complete the action in the event there is a mistake in your script.