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

Danny S 0 Reputation points

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,301 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.
522 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,427 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points

    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:

    There are PowerShell cmdlets for this:

    0 comments No comments