Table of Contents
Getting last logon date of all Office 365 Mailbox enabled users is one of the important task to track user logon activity and find inactive users to calculate the Exchange Online license usage. We can use the Exchange Online PowerShell cmdlet Get-MailboxStatistics to get last logon time, mailbox size, and other mailbox related statistics data.
Before proceed, first we need to connect Remote Exchange Online powershel module by running below command:
Run the following command to get mailbox statistics for a single Office 365 user
Get-MailboxStatistics -Identity [email protected] | Select LastLogonTime
PS C:\> Get-MailboxStatistics -Identity [email protected] | Select LastLogonTime
LastLogonTime
-------------
2/4/2023 9:22:34 AM
To find last logon time for all the Office 365 users, first we need to get all mailboxes by using Get-Mailbox cmdlet and pipe the results to Get-MailboxStatistics.
Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName,LastLogonTime
PS C:\> Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName,LastLogonTime
DisplayName LastLogonTime
----------- -------------
Ben 2/4/2023 9:22:34 AM
Bon Ben 2/4/2023 2:13:07 PM
Bon Ben 2/4/2023 9:38:07 AM
Chris 2/4/2023 10:35:2...
Info 1/11/2023 2:36:3...
...
Using above script, you can get output only from the cmdlet Get-MailboxStatistics and you can’t fetch any result from Get-Mailbox. If you want to read detail from Get-Mailbox command like UserPrincipalName, you need to merge output of two cmdlets.
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox
$mailboxes | ForEach-Object {
$mbx = $_
$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName | Select LastLogonTime
if ($mbs.LastLogonTime -eq $null){
$lt = "Never Logged In"
}else{
$lt = $mbs.LastLogonTime }
New-Object -TypeName PSObject -Property @{
UserPrincipalName = $mbx.UserPrincipalName
LastLogonTime = $lt }
}
#Output
UserPrincipalName LastLogonTime
----------------- -------------
[email protected] 2/4/2023 10:35:25 PM
[email protected] 2/4/2023 9:22:34 AM
[email protected] 1/28/2023 10:23:3...
[email protected] 2/4/2023 9:38:07 AM
[email protected] 2/4/2023 3:01:10 AM
...
Export Last Logon Time of Microsoft 365 Users to CSV file
You can also export all Exchange Online user’s last logon time to csv file by using below script
$Result=@()
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox
$totalmbx = $mailboxes.Count
$i = 1
$mailboxes | ForEach-Object {
$i++
$mbx = $_
$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName | Select LastLogonTime
if ($mbs.LastLogonTime -eq $null){
$LastLogonTime = "Never Logged In"
}else{
$LastLogonTime = $mbs.LastLogonTime }
Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
$Result += New-Object PSObject -property @{
Name = $mbx.DisplayName
UserPrincipalName = $mbx.UserPrincipalName
LastLogonTime = $LastLogonTime }
}
$Result | Export-CSV "C:\Report.csv" -NoTypeInformation -Encoding UTF8
As you see in the above the script, we are fetching all mailboxes first and we are iterating every user mailbox to find its mailbox statistics, so the process holds all the objects in memory. This kind of approach may leads some performance problem when you run the commands against bulk number of mailboxes (ex: over 20k users). So, if you face any performance issue, you can simply run the below command to get desired result. Here we are using Invoke-Command method, this command just pass the entire script block to Office 365 server and get only the result from server side.
Invoke-Command -ScriptBlock {
Get-Mailbox -RecipientTypeDetails UserMailbox -Resultsize Unlimited |
Get-MailboxStatistics |
Select-Object DisplayName,LastLogonTime} |
Export-CSV "C:\Result.csv" -NoTypeInformation -Encoding UTF8