This guide demonstrates how to use the Get-MgUserMailFolderMessage cmdlet in Microsoft Graph PowerShell to retrieve messages from specific mailbox folders. Learn how to filter messages, retrieve details, and export them with practical examples.
The Get-MgUserMailFolderMessage cmdlet is a powerful tool within the Microsoft Graph PowerShell module enabling administrators to retrieve messages from a specific mail folder in a user's mailbox. This article provides a detailed overview of the cmdlet including its syntax, usage examples, tips, use cases, possible errors, and solutions.
Get-MgUserMailFolderMessage -UserId <String> -MailFolderId <String> [-Filter <String>] [-Search <String>]
Note: You need -MailFolderId to work with this cmdlet which can got by executing Get-MgUserMailFolderMessage and passing in the UserID or UPN.
This command retrieves all messages from the specified folder.
Get-MgUserMailFolderMessage -UserId "user@domain.com" -MailFolderId "AAMkAGI2T"
This command retrieves messages received after January 1, 2024, from the specified folder.
Get-MgUserMailFolderMessage -UserId "user@domain.com" -MailFolderId "AAMkAGI2T" -Filter "receivedDateTime ge 2024-01-01T00:00:00Z"
This command searches for messages containing the word "Important" in the specified folder.
Get-MgUserMailFolderMessage -UserId "user@domain.com" -MailFolderId "AAMkAGI2T" -Search "Important"
This command searches for the message based on the message ID passed to -MessageID.
Get-MgUserMailFolderMessage -UserId "user@domain.com" -MailFolderId "AAMkAGI2T" -MessageId "AAMkADA"
Cause: The specified user ID or UPN is incorrect or does not exist.
Solution: Verify that the -UserId parameter is correct and corresponds to an existing user. Use Get-MgUser to check user details.
Get-MgUser -UserId "user@domain.com"
Cause: The specified mail folder ID is incorrect or does not exist.
Solution: Verify that the -MailFolderId parameter is correct. Use Get-MgUserMailFolder to retrieve valid folder IDs.
Get-MgUserMailFolder -UserId "user@domain.com"
Cause: The executing user does not have the necessary permissions to access the specified mail folder.
Solution: Ensure that the executing user has the required permissions. This might involve granting appropriate permissions or running the cmdlet as an administrator. Mail.Read is the required Graph API permission.
Cause: The request is being throttled due to high usage or rate limits.
Solution: Implement retry logic in the script and adhere to Microsoft's throttling guidelines.
Start-Sleep -Seconds 30
1. What is Get-MgUserMailFolderMessage used for?
Get-MgUserMailFolderMessage is a Microsoft Graph PowerShell cmdlet used to retrieve email messages from a specific folder in a user’s mailbox, such as Inbox or Sent Items.
2. How can I retrieve all messages from a specific folder?
Use the following script to list all messages in a folder:
Get-MgUserMailFolderMessage -UserId "<UserPrincipalName>" -MailFolderId "<FolderId>" -All
3. Can I filter messages by subject or sender?
Yes, use the -Filter parameter to filter messages. Example for filtering by subject:
Get-MgUserMailFolderMessage -UserId "<UserPrincipalName>" -MailFolderId "<FolderId>" -Filter "subject eq 'Important Update'" -All
4. How can I export folder messages to a CSV file?
Use this script to export messages from a specific folder:
$Messages = Get-MgUserMailFolderMessage -UserId "<UserPrincipalName7gt;" -MailFolderId "<FolderId>" -All
$Messages | Select-Object Subject, Sender, ReceivedDateTime | Export-Csv -Path "C:\Path\To\FolderMessages.csv" -NoTypeInformation
5. What permissions are required to retrieve folder messages?
You need the Mail.Read or Mail.ReadWrite permission in Microsoft Graph PowerShell. Ensure these permissions are granted and consented in Azure AD.
The Get-MgUserMailFolderMessage cmdlet is a versatile tool for managing and retrieving messages from user mail folders within Microsoft 365. By understanding its syntax, usage, and potential pitfalls, administrators can effectively leverage this cmdlet for various use cases, from auditing to automated reporting. Remember to handle possible errors gracefully and optimize your queries for performance.
© m365corner.com. All Rights Reserved. Design by HTML Codex