How to Export Microsoft 365 Mailboxes Inactive for 90 Days
Question details
The user needs to generate and export a CSV report containing Microsoft 365 mailboxes that have not been logged into for more than 90 days.

- Product
- Microsoft 365
- Device & OS
- not provided
- Scenario
- Administering Microsoft 365 environments and attempting to track unused licenses or inactive user accounts via PowerShell.
- Observed behavior
- The current PowerShell pipeline fails to generate a CSV file and throws an error stating that the mailbox name is not unique.
Ensure you have the Exchange Online PowerShell module installed and that you are connected using an administrator account with the necessary permissions.
Use UserPrincipalName in Exchange Online PowerShell
Resolve the unique identifier error by passing the UserPrincipalName to Get-MailboxStatistics, then filter and export the results to a CSV file.
When retrieving mailbox statistics, using the default identity can sometimes result in conflicts if multiple mailboxes share similar display names or aliases. Using the UserPrincipalName guarantees a unique identifier for each account, allowing the pipeline to process successfully.
Open PowerShell as an administrator and run the command 'Connect-ExchangeOnline' to authenticate with your Microsoft 365 admin credentials.
Execute the following command to retrieve, filter, and export the mailboxes: Get-Mailbox -ResultSize Unlimited | ForEach-Object { Get-MailboxStatistics -Identity $_.UserPrincipalName } | Where-Object {$_.LastLogonTime -lt (Get-Date).AddDays(-90)} | Select-Object DisplayName,LastLogonTime,MailboxTypeDetail | Export-Csv 'C:\InactiveMailboxes.csv' -NoTypeInformation
Navigate to your C: drive and open the 'InactiveMailboxes.csv' file using your preferred spreadsheet application to review the inactive accounts.

Use the Microsoft 365 Compliance Center Audit Logs
An alternative method to check for user inactivity without running PowerShell scripts.
Easily Analyze Your Exported CSV Reports with WPS Office
While Microsoft 365 requires complex PowerShell scripts to manage mailboxes, you need a reliable spreadsheet tool to view the results. WPS Office is a free, lightweight suite that makes filtering and analyzing your exported administrative CSV reports incredibly easy.
- 1. Download WPS Office: Visit the official WPS website to download and install the free office suite.
- 2. Open WPS Spreadsheet: Launch the application and select the Spreadsheets module.
- 3. Analyze the CSV Report: Open your exported 'InactiveMailboxes.csv' file to easily view, format, and filter your Microsoft 365 data.

Frequently Asked Questions
Why does Get-MailboxStatistics return a 'mailbox name is not unique' error?
This error occurs when the script relies on standard display names or aliases, which might be duplicated across your organization. By explicitly calling the UserPrincipalName ($_.UserPrincipalName), you provide a universally unique identifier that prevents this conflict.
Do I need special permissions to run these PowerShell commands?
Yes, you must be assigned appropriate administrative roles in Microsoft 365, such as Organization Management or Recipient Management, to run Get-Mailbox and Get-MailboxStatistics.
Can I change the inactivity threshold from 90 days to something else?
Absolutely. In the PowerShell script provided, simply change the '-90' in the AddDays(-90) method to your desired number of days (for example, -30 for 30 days of inactivity).
What is the best way to open the exported CSV file?
You can open the generated CSV file using a dedicated spreadsheet application like WPS Office Spreadsheets or Microsoft Excel. This allows you to adjust column widths, sort by LastLogonTime, and format the data properly.




