Get-MgUserMessage

What is Get-MgUserMessage?

The Get-MgUserMessage cmdlet in Microsoft Graph PowerShell retrieves email messages from a Microsoft 365 user’s mailbox. It allows administrators to fetch all messages, individual emails, or filtered sets of messages based on properties such as subject, sender, or received date.


Why Use Get-MgUserMessage?

This cmdlet is useful for:

  • Automating mailbox reporting.
  • Auditing email activity for compliance or security.
  • Retrieving emails for troubleshooting.
  • Exporting targeted sets of messages for review.

By leveraging filters and search parameters, administrators can precisely extract the data they need instead of manually reviewing entire mailboxes.


Prerequisites

Before using this cmdlet, connect to Microsoft Graph with the appropriate permissions:

Connect-MgGraph -Scopes "Mail.Read"

How to Use Get-MgUserMessage?

Syntax (essential parameters):

Get-MgUserMessage -UserId <String> [-MessageId <String>]

This cmdlet can be used to retrieve all messages for a user, fetch a single message by its ID, or apply filters for refined queries.


Get-MgUserMessage Examples

  • Retrieve All Messages for a User
  • Get-MgUserMessage -UserId "john.doe@contoso.com"

    This command retrieves all messages for the specified user.

  • Retrieve a Specific Message by Message ID
  • Get-MgUserMessage -UserId "john.doe@contoso.com" -MessageId "AAMkAGI2T..."

    This command retrieves a specific message by its message ID.

  • Retrieve Messages with Specific Properties
  • $User = "samadmin@7xh7fj.onmicrosoft.com"
    
    Get-MgUserMessage -All -UserId "$User" |
    Select-Object Subject, InternetMessageId, ReceivedDateTime,
    @{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address }},
    @{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ' ' }} |
    Out-GridView
                                                

    This command retrieves messages with selected properties and displays them in a grid view.

  • Filter Messages by Subject
  • Get-MgUserMessage -UserId "john.doe@contoso.com" -Filter "contains(subject,'Project Update')"

    This command retrieves messages that contain “Project Update” in the subject.

  • Search Messages
  • Get-MgUserMessage -UserId "john.doe@contoso.com" -Search "Project Update"

    This command searches for messages containing “Project Update.”

  • Retrieve a Limited Number of Messages
  • Get-MgUserMessage -UserId "john.doe@contoso.com" -Top 10

    This command retrieves the top 10 messages for the specified user.

  • Retrieve Emails Received from a Specific Sender
  • Get-MgUserMessage -UserId "user@domain.com" -Filter "from/emailAddress/address eq 'ceo@domain.com'" -Top 10

Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex