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.DisplayNameItems = $Stats.ItemCountSize = $MbxSizeLastLogon = Get-Date($Stats.LastLogonTime) -Format gLastActive = Get-Date($Stats.LastInteractionTime) -Format gLastEmail = Get-Date($LastEmail) -Format gLastCalendar = Get-Date($LastCalendar) -Format gLastContacts = Get-Date($LastContacts) -Format gLastFile = Get-Date($LastFile) -Format g}$Report.Add($ReportLine)}$Report | Export-csv -NoTypeInformation Users.csv$Report | Sort {$_.LastLogon -as [datetime] -descending | Out-GridView
Attribution
Author
Office365itpros