We can use the Exchange powershell cmdlet Get-MailboxStatistics (On-premises and Online) to check the Last logon time of an user’s mailbox. In this post I am going share powershell commands to find and get a list of active users who are actively using their mailbox in Office 365 environment.
Before proceed, first we need to connect Exchange Online powershel module by running below commands:
$o365Cred = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365Cred -Authentication Basic -AllowRedirection Import-PSSession $Session
You can find last logon time for a single user mailbox by running below command:
Get-MailboxStatistics -Identity "[email protected]" | Select LastLogonTime
If you want to get last logon time for all the Office 365 mailbox users, first we need to get all mailbox details by using Get-Mailbox cmdlet and pipe the results to Get-MailboxStatistics.
Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName,LastLogonTime
Find active mailboxes in last N days
To get active mailbox list we need to use Where filter in the output of Get-MailboxStatistics. The below powershell command find and retrieve all mailbox users who are logged-into their mailbox within last 7 days.
Get-Mailbox -RecipientType 'UserMailbox' -ResultSize Unlimited | Get-MailboxStatistics | Where {$_.LastLogonTime –gt ([System.DateTime]::Now).AddDays(-7) } | Sort-Object LastLogonTime -Descending | Format-Table DisplayName, LastLogonTime
Export list of active mailbox users to CSV file
The below command find and export list of active mailbox user names and their last logon time to CSV file.
Get-Mailbox -RecipientType 'UserMailbox' -ResultSize Unlimited | Get-MailboxStatistics | Where {$_.LastLogonTime –gt ([System.DateTime]::Now).AddDays(-7) } | Sort-Object LastLogonTime -Descending | Select DisplayName, LastLogonTime | Export-CSV "C:\ActiveMailboxes.csv" -NoTypeInformation -Encoding UTF8
Note: Here I have used number of days as 7 to check logon activity, you can change this value (i.e 30 or 90 days) as per your need and you can also use the same commands for On-premise environment by properly connecting Exchange management powershell.