Powershell Office 365 emails

SJ Hirani 1 Reputation point
2021-01-25T14:42:52.193+00:00

Does anybody know if below is possible by Powershell - I have asked Microsoft support they said it is not possible.

A way of seeing the top 100 emails (or emails over 10mb) in a given shared / group mailbox - so as to check if people are sending / receiving files that are massive. | Sender | Recipient | Date | Email size in MB | and export it to a .CSV file

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,416 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 45,186 Reputation points
    2021-01-25T15:37:09.707+00:00

    How about Get-EXOMailboxFolderStatistics?

    0 comments No comments

  2. SJ Hirani 1 Reputation point
    2021-01-25T15:39:11.94+00:00

    Thanks for your help Rich
    Only need top 100 highest email size, If i do that it will not show me each email size.


  3. Rich Matheisen 45,186 Reputation points
    2021-01-26T16:04:48.88+00:00

    If you're doing this from a machine on which Outlook is installed you can try using this instead of EWS or trying to get Invoke-RestMethod (and having to deal with OAUTH2 authentication). It's only going to look in the "Default" folder in this example, but you can change that if the messages are in some other folder. If you want to deal only with messages over 10MB just add a Select-Object just before the Foreach-Object to check for "$_.Size -gt 10MB".

    Add-Type -assembly "Microsoft.Office.Interop.Outlook"
    Add-Type -assembly "System.Runtime.Interopservices"
    try
    {
        $outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
        $outlookWasAlreadyRunning = $true
    }
    catch
    {
        try
        {
            $Outlook = New-Object -comobject Outlook.Application
            $outlookWasAlreadyRunning = $false
        }
        catch
        {
            write-host "You must exit Outlook first."
            exit
        }
    }
    $namespace = $Outlook.GetNameSpace("MAPI")
    $inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    $inbox.Items |
        ForEach-Object{
            [PSCustomObject]@{
                Subject=$_.Subject
                Size=$_.Size
                From=$_.SenderEmailAddress
            }
        } |
            Sort-Object Size -Descending |
                Select-Object -First 100
    # Close outlook if it wasn't opened before running this script
    if ($outlookWasAlreadyRunning -eq $false)
    {
        Get-Process "*outlook*" | Stop-Process -force
    }
    
    0 comments No comments