Skip to content

Instantly share code, notes, and snippets.

@amit-g
Last active February 20, 2023 18:34
Show Gist options
  • Save amit-g/af28b50d5e47836f8f419550e8e31b5c to your computer and use it in GitHub Desktop.
Save amit-g/af28b50d5e47836f8f419550e8e31b5c to your computer and use it in GitHub Desktop.
Outlook Junk Email Data
Connect-MgGraph -UseDeviceAuthentication -ContextScope Process -Scopes "Mail.Read", "Mail.ReadWrite", "Mail.ReadBasic"
Get-MgUser
$UserId = (Get-MgUser).Id
Get-MgUserMailFolder -UserId $userId -Filter "startswith(displayName, 'Junk')" | Format-List
$MailFolderId = (Get-MgUserMailFolder -UserId $userId -Filter "startswith(displayName, 'Junk')" | Select-Object -First 1).Id
Get-MgUserMailFolderMessage -UserId $userId -MailFolderId $MailFolderId -Property "sender, subject" -Top 1000 | Select-Object @{ n='EmailDomain'; e={$_.Sender.EmailAddress.Address -Replace '[^@]+@', ''} }, @{ n='EmailAddress'; e={$_.Sender.EmailAddress.Address;} }, @{ n='Name'; e={$_.Sender.EmailAddress.Name} }, @{ n='Subject'; e={$_.Subject;} } | ConvertTo-Csv -NoTypeInformation > OutlookJunk-$(Get-Date -Format 'yyyyMMdd').csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment