A Quick PowerShell Approach to Remove Exchange Online Mailboxes in Microsoft 365

A Quick PowerShell Approach to Remove Exchange Online Mailboxes in Microsoft 365

When users leave the organization, it is crucial to remove Exchange Online mailboxes in order to free up Microsoft 365 licenses assigned to them. Even the Office 365 admin center provides a user-friendly interface for mailbox management, it lacks distinct advantages and flexibility. However, with PowerShell, admins can efficiently manage or remove the Exchange Online mailboxes in Microsoft 365.

This blog provides guidance and best practices for a smooth and effective mailbox removal process using PowerShell. Let’s explore the process of removing Exchange Online mailboxes when you have inactive or unused user accounts in your Microsoft 365 environment.

What are the Reasons to Delete an Exchange Online Mailbox?

The purpose of removing mailboxes may vary with respect to circumstances. The following are the common reasons to remove Office 365 mailboxes.

Microsoft 365 Mailbox Migration: When a user migrates from your Office 365 tenant to another tenant, you need to remove Exchange Online mailboxes to free up resources.

Off-boarding Employees: During Microsoft 365 off-boarding, their mailboxes will be removed to maintain data security. Removing the mailbox ensures that the user can no longer access company emails, calendars, and other data. If you want to ensure the offboarded user has no access left, you can track offboarded user activity using PowerShell.

Exchange Online Mailbox Cleanup: Removing a mailbox helps to keep the Exchange Online environment clean and organized, mainly when dealing with inactive mailboxes.

Microsoft 365 License Management: Exchange Online mailboxes are basically associated with user licenses. When a user no longer requires a mailbox, removing it allows to maintain effective Microsoft 365 license management. You can then allocate it to another user to avoid unnecessary license expenditures.

What Happens When You Remove a Mailbox in Exchange Online?

When you remove a mailbox in Exchange Online, the following actions typically occur:

  • First, it becomes disconnected from its user account and is no longer available for sending or receiving emails.
  • The disconnected mailbox is hidden and flagged for removal.
  • The mailbox remains flagged until it is permanently deleted (purged), which typically happens after a specified mailbox retention period (usually 30 days).

Note: Prior to purging, the disconnected mailbox can be reconnected to a new or existing user account that doesn’t already have a mailbox associated with it. So that if you wish, you can restore the deleted mailbox before the retention period.

Stages of Mailbox Deletion in Exchange Online

The state of mailbox deletion in Exchange Online refers to the specific status or condition of a mailbox after it has been deleted. This state includes stages such as soft deletion and hard deletion.

1. Soft Deletion: A soft-deleted user mailbox refers to a mailbox that has been removed either through the Microsoft 365 admin center or PowerShell. After deletion, it remains in the Azure AD recycle bin for 30 days. Here are the possible cases in which mailboxes will be soft-deleted.

  • The associated Azure AD account of the user mailbox has normally been deleted. This means that the user object is either out of scope or located within the recycle bin container.
  • When the Azure AD user account associated with the mailbox has been hard deleted, the Exchange Online mailbox remains under litigation hold or eDiscovery hold.
  • If the Azure AD user account associated with the user’s mailbox has been purged within the last 30 days.

2. Hard Deletion: A hard-deleted user mailbox refers to a mailbox that cannot be recovered or restored. The mailbox enters a hard-deleted state under the following circumstances:

  • The user mailbox remains in a soft-deleted state for over 30 days after the associated Azure AD user has been hard-deleted. During this time, all mailbox content, including emails, contacts, and files, is permanently removed.
  • When the user account linked to the user mailbox is permanently deleted using the PowerShell cmdlets.

Point to Remember: It is important to note that user mailboxes cannot be deleted or restored using the Exchange Admin Center.

Delete Exchange Online Mailbox Using PowerShell

You have the flexibility to remove Exchange Online mailboxes through PowerShell using the following ways.

Before proceeding, you must install and connect to Exchange Online PowerShell module to use the cmdlets described in this blog.

Soft Delete an Exchange Online Mailbox Using PowerShell

You can soft delete the mailbox using the ‘Remove-Mailbox‘ cmdlet, as mentioned below.

Remove-Mailbox –Identity <UPN> 

To execute the above cmdlet without prompting for confirmation, simply add the “-Confirm:$false” parameter at the end of the cmdlet. This parameter will prevent any confirmation prompts from appearing during the execution of the cmdlet.

Note: You can also remove resource mailboxes, such as room mailboxes and equipment mailboxes, using the “Remove-Mailbox” cmdlet.

View Office Soft Deleted Mailbox in Office 365

Listing soft-deleted mailboxes in Office 365 using PowerShell helps to identify mailboxes that have been deleted but are still recoverable within a retention period.

You can use the following cmdlet to get the list of all soft-deleted mailboxes.

Get-Mailbox –SoftDeletedMailbox

List soft-deleted office 365 mailboxes

Restore Soft Deleted Mailbox Using PowerShell

When a mailbox is accidentally deleted, it can be restored within the retention period. You can restore a soft-deleted mailbox using the following cmdlet.

Undo-SoftDeletedMailbox <UPN>

estore soft-deleted user using PowerShell

The above cmdlet restores only the soft deleted mailboxes. However, once the mailboxes have been removed permanently, they won’t be restored.

Bulk Delete Exchange Online Mailboxes in Office 365

To bulk delete Office 365 mailboxes using PowerShell, you must first prepare a CSV file containing a list of mailboxes to be removed. The CSV file should have a column header EmailAddress” that specifies the email addresses of the mailboxes to remove. CSV File Format for Bulk Removal of Mailboxes

After the creation of CSV file, run the following cmdlet in the Exchange Online PowerShell by replacing <FileLocation> with the appropriate location of the CSV file.

Import-Csv "<FileLocation>" | ForEach-Object { 
    Write-host $_.EmailAddress 
    Remove-Mailbox -identity $_.EmailAddress -confirm:$false 

The above PowerShell script performs a bulk soft deletion of the mailboxes associated with the email addresses specified in the CSV file. It also displays the list of soft-deleted mailboxes at the respective deletion.

Bulk Delete Exchange Online Mailboxes

Hard Delete a Mailbox in Office 365 with PowerShell

Permanent force delete or hard delete refers to the complete elimination of Office 365 mailboxes that are no longer required.

To permanently hard delete an Office 365 mailbox using PowerShell, you can use the ‘Remove-Mailbox‘ cmdlet with the ‘PermanentlyDelete‘ parameter.

Remove-Mailbox –Identity <UPN> -PermanentlyDelete:$true -Confirm:$false

However, before executing the above cmdlet, ensure that you have permanently deleted the corresponding user from the Azure Active Directory. It will take a few minutes to delete a user in the Azure AD permanently. You can also use the following Graph cmdlets to delete the user using Microsoft Graph PowerShell module.

1. You can execute the cmdlet below to move the user to recycle bin.

Remove-MgUser -UserId <UserId>  

Note: you can get the “UserId” of the respective user by the ‘Get-MgUser’ cmdlet.

2. To delete a user from the recycle bin, you can execute the below cmdlet.

Remove-MgDirectoryDeletedItem -DirectoryObjectId <UserId>

The “UserId” becomes “DirectoryObjectId” when the user account enters the recycle bin directory.

Permanently Delete a Soft-Deleted Mailbox in Office 365

You can hard delete a soft-deleted mailbox by permanently removing the corresponding user from Azure AD.

Use the below PowerShell cmdlet to permanently delete a soft-deleted mailbox in Office 365.

Get-Mailbox -Identity <UPN> -SoftDeletedMailbox | Remove-Mailbox -PermanentlyDelete -Confirm:$false 

Replace the UPN with the user principal name of the soft-deleted mailbox.

Move Closer with Microsoft 365 Inactive Mailbox Management Using AdminDroid!

To facilitate the removal of Microsoft 365 mailboxes, it is vital to monitor inactive mailboxes in Exchange Online. But PowerShell lacks comprehensive information on monitoring inactive Exchange Online mailboxes. On the other hand, you can elevate your inactive mailbox reporting capabilities by utilizing the AdminDroid Microsoft 365 reporting tool.

AdminDroid’s Exchange Online inactive mailbox reports allow admins to discover all Microsoft 365 inactive mailboxes and their details with advanced insights on mailbox last logon time, mailbox size, type, and more. Following is the list of Exchange Online inactive mailbox reports provided by AdminDroid.

  • Inactive Mailboxes by Last Logon Time
  • Mailboxes by Users’ Last Activity
  • Never Logged in Microsoft 365 Inactive Mailboxes
  • Exchange Inactive Mailboxes by Last Mail Read
  • Inactive Mailboxes by Last Mail Sent
  • EXO Inactive Mailboxes by Last Mail Received

All Inactive Mailbox report - AdminDroid

Never Logged In Mailboxes Report - AdminDroid

In addition to delivering detailed extensive reports on inactive and unused mailboxes, the Exchange Online reporting tool excels in providing precise metrics related to mailbox usage, mailbox permission, mailbox setting details and more.

Also, AdminDroid provides 65+ in-depth Exchange Online audit reports on mailbox activities, mailbox access permissions, and mailbox configuration changes to prevent unauthorized usage of mailboxes in Microsoft 365.

The Exchange Online dashboard offers a comprehensive overview of your Exchange Online environment, presenting essential details such as mailbox type, size and quota, usage, etc., necessary for effective mailbox monitoring.

With outstanding capability, AdminDroid’s Exchange Online management tool offers 170+ insights and auditing reports, to gain deep aspects of the Exchange Online environment.

Are you curious to know why AdminDroid stands out from the rest?

Here it is! AdminDroid also provides a 360° view of all Microsoft 365 services. With a vast collection of 1800+ reports and 30+ visually appealing smart dashboards, AdminDroid ensures you have a advanced understanding of your entire Microsoft 365 environment.

Experience the benefits by downloading AdminDroid Office 365 reporting tool and explore all the features with a 15-day free trial.

I hope this blog has provided a simple PowerShell Solution for managing unused mailboxes by removing them from your Microsoft 365 environment. Feel free to reach us in the comments section for any assistance.

A Quick PowerShell Approach to Remove Exchange Online Mailboxes in Microsoft 365

by Thiraviam time to read: 6 min