How can I use powershell to count emails by sender for a large (160k+) O365 mailbox?

Danny S 0 Reputation points
2023-05-07T17:40:22.2633333+00:00

I have a large mailbox (160k+ emails) on Microsoft 365 Business Premium that's running out of space.

The outlook clients (online and mac desktop) are hopeless for grouping and deleting emails in bulk, so I want to run powershell scripts (or some other kind of code/scripts) that will:

  • Give me a summary of the count of emails per sender, ideally with a first and last sent date, so I can work out which senders has sent the most emails to the mailbox.
  • Allow me to delete all the emails from a given sender (or list of senders in an array or csv file) so I can free up space in the mailbox.

I am a developer but I don't really know powershell - I've tried Get-MessageTrace but it's limited to the last 10 days, and I can't work out if Start-HistoricalSearch will be able to give me what I need.

I'm happy to try another method other than powershell if there's another approach.

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,388 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
529 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2023-05-07T18:12:56.8+00:00

    For an on-premises Exchange we used to use MAPI (i.e., the Outlook COM API). With O365 your best bet is to use Microsoft Graph: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/office-365-rest-apis-for-mail-calendars-and-contacts

    There are PowerShell cmdlets for this: https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.mail/?view=graph-powershell-1.0

    0 comments No comments