Export last log on more 90 days Office 365



I'm trying to export csv file, with users that not logged on more then 90 days.

I tried to run this script:


Get-Mailbox -RecipientType 'UserMailbox' | Get-MailboxStatistics | Sort-Object LastLogonTime | Where {$_.LastLogonTime –lt ([System.DateTime]::Now).AddDays(-90) } | Export-Csv "D:\Logs\O365MAILBOXSTATS_REPORT.CSV" -NoTypeInformation

I didn't received any csv file after the script finish.

Also, I got this errors :

Get-MailboxStatistics: The specified mailbox "User name"  isn't unique.


My goal is to get users that not connected to O365 more then 90 days.

8 Replies

Hello @dannytveria,

Your error means that PowerShell is trying to use not unique value for -Identity parameter of Get-MailboxStatistics cmdlet.
For Identity parameter you can use any value that uniquely identifies the mailbox. For example:

  • Name
  • Alias
  • Distinguished name (DN)
  • Canonical DN
  • Domain\Username
  • Email address
  • GUID
  • LegacyExchangeDN
  • SamAccountName
  • User ID or user principal name (UPN)


In your case one of the options would be to use UPN as it is unique within your organization.

You need to modify your script to explicitly pass UserPrincipalName to Get-MailboxStatistics.

Hope that helps.


Hi Andy,

thanks for your response.

I didn`t understand where exactly I need to change value.

can you please help me with my example?

Hello @dannytveria,

Try this:

Get-Mailbox -RecipientType 'UserMailbox' |%{ Get-MailboxStatistics $_.UserPrincipalName | Sort-Object LastLogonTime | Where {$_.LastLogonTime -lt ([System.DateTime]::Now).AddDays(-90) } | Export-Csv "D:\Logs\O365MAILBOXSTATS_REPORT.CSV" -NoTypeInformation -Append}

Hope that helps.

Hi Andy,
thanks for the help.
I getting a warning:
By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned.

Also, no CSV file not created maybe because of the warning?

Hello @dannytveria,

Add -ResultSize Unlimited to Get-Mailbox:

Get-Mailbox -RecipientType 'UserMailbox' -ResultSize Unlimited | ...

Also try to remove filter with LastLogonDate  just to test of that it works.

It might be that there are no users who accessed their mailbox more that 90 days ago.

Hope that helps.

It`s worked with the LastLogonDate.
the LastLogonDate field is when the user last login to his mailbox on 365?

Or which field actually shows the last use mailbox?



The research was done on many real-time users, and we have concluded that getting Office 365 user’s inactive time from LastUserActionTime is the best.


Reference: https://o365reports.com/2019/06/18/office-365-users-last-logon-time-incorrect/


Hope that helps.


Hi Andy,
thanks for your gr8 help.
U was very helpfull