Easily find the unused mailboxes in your tenant

When you are running a large Office 365 environment, there are probably a lot of employees that form all sorts of project groups, focus groups, task forces, and such more. Chances are you have accumulated a lot of shared mailboxes over the years. From my personal experience: they are often requested but seldom removed after a project has ended.

This could become a bad thing. There could be sensitive company data or GDPR related data in that unmonitored mailbox that is best deleted.

This is a Powershell script that will dump all your shared mailboxes and the time/date of the most recent item in that mailbox in a CSV file. The most recent item is the last item that was received or changed in that mailbox. An inactive mailbox that nobody is using typically has nothing going in or out and will show up with a date (far) in the past. These mailboxes can be subject to removal or further investigation.

Powershell script to find unused mailboxes

Please make sure you have installed the Exchange Online Powershell module.

# Fill in the full path and filename
$path = "c:\filename.csv"
$counter = 0

# Connect to O365
Connect-ExchangeOnline

$mb = Get-EXOMailbox -RecipientTypeDetails Sharedmailbox -ResultSize unlimited

Write-Host $mb.Count items.

foreach ($m in $mb) 
{ 
Write-Host Checking: $m.PrimarySmtpAddress - $counter
$counter ++
Get-EXOMailboxFolderStatistics -identity $m.PrimarySmtpAddress -IncludeOldestAndNewestItems | sort newestItemReceivedDate | where {$_.newestItemReceivedDate -ne $null} | select Identity,newestItemReceivedDate -last 1 | Export-Csv $path -Append -NoTypeInformation
}

The script will go over all shared mailboxes. This will be a slow process and for that reason I included some sort of progress indicator. The script will print the number of mailboxes it is going to check and after that, for each mailbox it will print the name and sequence number. That way you can see the script is working while it is processing the mailboxes. Depending on the time of day and size of the mailboxes it takes anywhere from 3 to 30 seconds per mailbox.

The result is a CSV file with 2 columns that will help you identify the unused mailboxes in your tenant.

unused mailboxes in csv
Find the unused mailboxes

I hope this is helpful information to whoever decides to use it.