Back to script library
Entra / Microsoft 365 · Exchange Online

Get last active time mailboxes

Report last active time for mailboxes.

Connect & set up

Run these once per session. All scopes are read-only unless the script makes changes.

# Review required modules and connection steps before running.
# Connect to Microsoft Graph or Exchange Online as needed for this script.

Run it

The main script. Copy it, or download the .ps1 and run it from your console.

$Mbx = (Get-ExoMailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited | Select DisplayName, DistinguishedName)
$Report = [System.Collections.Generic.List[Object]]::new()
ForEach ($M in $Mbx) {
Write-Host "Processing" $M.DisplayName
$Log = Export-MailboxDiagnosticLogs -Identity $M.DistinguishedName -ExtendedProperties
$xml = [xml]($Log.MailboxLog)
$LastEmail = ($xml.Properties.MailboxTable.Property | ? {$_.Name -like "LastEmailTimeCurrentValue"}).Value
$LastCalendar = ($xml.Properties.MailboxTable.Property | ? {$_.Name -like "LastCalendarTimeCurrentValue"}).Value
$LastContacts = ($xml.Properties.MailboxTable.Property | ? {$_.Name -like "LastContactsTimeCurrentValue"}).Value
$LastFile = ($xml.Properties.MailboxTable.Property | ? {$_.Name -like "LastFileTimeCurrentValue"}).Value
$Stats = (Get-MailboxStatistics -Identity $M.DistinguishedName)
$MbxSize = ($Stats.TotalItemSize.Value.ToString()).Split("(")[0]
$ReportLine = [PSCustomObject]@{
Mailbox = $M.DisplayName
Items = $Stats.ItemCount
Size = $MbxSize
LastLogon = Get-Date($Stats.LastLogonTime) -Format g
LastActive = Get-Date($Stats.LastInteractionTime) -Format g
LastEmail = Get-Date($LastEmail) -Format g
LastCalendar = Get-Date($LastCalendar) -Format g
LastContacts = Get-Date($LastContacts) -Format g
LastFile = Get-Date($LastFile) -Format g}
$Report.Add($ReportLine)}
$Report | Export-csv -NoTypeInformation Users.csv
$Report | Sort {$_.LastLogon -as [datetime] -descending | Out-GridView
Attribution