Find inactive mailboxes in Exchange Online
Posted On January 31, 2020
So you want to clean up unused (shared) mailboxes in your Exchange (Online) environment. How to find out which mailboxes have been inactive for a long time? The answer is yet simple again, with a cool Power Shell script.

First we will connect to Exchange Online
$Credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Now, if you want to check a single mailbox you can use the following line. By using the get-mailboxfolderstatistics command we can retrieve when the last email has been received or send.
Get-MailboxFolderStatistics obsoletemailbox@2azure.nl -IncludeOldestAndNewestItems | sort newestItemReceivedDate | where {$_.newestItemReceivedDate -ne $null} | select Identity,newestItemReceivedDate -last 1
With a few additions you can check all your mailboxes in your environment. With the get-mailbox command we will retrieve a list of all mailboxes. By looping through we should get the result for all users in a good readable format.
$mailboxes = get-mailbox
foreach ($user in $mailboxes){
Get-MailboxFolderStatistics $user.name -IncludeOldestAndNewestItems | sort newestItemReceivedDate | where {$_.newestItemReceivedDate -ne $null} | select Identity,newestItemReceivedDate -last 1
}