Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active March 3, 2023 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-long/a0b889e49c0edb935a7ba61b25b363b7 to your computer and use it in GitHub Desktop.
Save bill-long/a0b889e49c0edb935a7ba61b25b363b7 to your computer and use it in GitHub Desktop.
#Requires -Modules Microsoft.Graph, Microsoft.Graph.Users.Actions
Connect-MgGraph -Scopes "User.Read.All", "Mail.ReadBasic", "Mail.Read", "Mail.ReadWrite" -DeviceAuth -ContextScope Process
$userId = (Invoke-MgGraphRequest -Uri "v1.0/me")["id"]
# Only returns 10 items by default
Get-MgUserMessage -UserId $userId | ft ReceivedDateTime,Subject
# You can filter for stuff older than a certain date
Get-MgUserMessage -UserId $userId -Filter "ReceivedDateTime lt 2023-01-25" | ft ReceivedDateTime,Subject
# Then you can pipe that to foreach and perform some action.
# In this example we move the first result to the Archive folder (the one-click archive, NOT the archive mailbox).
Get-MgUserMessage -UserId $userId -Filter "ReceivedDateTime lt 2023-01-25" | Select -First 1 | % { Move-MgUserMessage -DestinationId archive -UserId $userId -MessageId $_.Id }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment