Forum Discussion

dannytveria's avatar
dannytveria
Brass Contributor
Apr 25, 2021

Export last log on more 90 days Office 365

Hi,

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.

  • AndySvints's avatar
    AndySvints
    Steel Contributor

    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.

    • dannytveria's avatar
      dannytveria
      Brass Contributor

      AndySvints 

      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?

      • AndySvints's avatar
        AndySvints
        Steel Contributor

        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.

Resources