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
}