Fix PowerShell Invoke-RestMethod Errors for Large Exchange Online Attachments
Question details
The user needs to download large Exchange Online email attachments and process all available messages with PowerShell.

- Product
- Exchange Online and PowerShell Invoke-RestMethod
- Device & OS
- not provided
- Scenario
- A PowerShell script retrieves messages and attachments from Exchange Online.
- Observed behavior
- The script may return HTTP 400 for attachments larger than 10 MB and may process only a limited number of messages.
Confirm the Exchange Online endpoint, authentication method, and whether the affected attachments are file attachments. Capture the HTTP response body and request ID from a failed request, but remove access tokens and private account details before sharing logs.
Download file attachments through Microsoft Graph and follow message pages
Use this approach when the current API workflow fails on large file attachments or stops after the first page of messages.
An attachment exceeding 10 MB is not, by itself, enough to identify the cause of an HTTP 400 error. Check the response and endpoint rather than assuming a universal 10 MB download limit.
The /me endpoint requires delegated authentication. For app-only authentication, use the appropriate /users/{id} endpoint and application permissions.
Register or select an app in Microsoft Entra ID, grant the appropriate Microsoft Graph Mail.Read permission, and obtain an access token with the required consent.
Call GET https://graph.microsoft.com/v1.0/me/messages with the bearer token. Process the response's value array, then request each @odata.nextLink URL unchanged until no next link is returned.
For each message, call GET /me/messages/{message-id}/attachments and identify the attachment ID and type. Follow any @odata.nextLink returned by the attachment list.
For a file attachment, request GET /me/messages/{message-id}/attachments/{attachment-id}/$value. In PowerShell, keep $value literal in the URL, for example by using a single-quoted string, and use Invoke-RestMethod with the Authorization header and -OutFile to save the response.
Open the output file and compare its size with the attachment metadata. Log the message and attachment IDs and response details for any failed request; if Graph returns HTTP 429, honor its Retry-After header before retrying.

Diagnose the existing PowerShell request and paging logic
Use the response details and script review to find the cause before replacing an existing Exchange Online integration.
Open Office Documents from Email Attachments with WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for working with downloaded documents. Its Microsoft Office format compatibility makes it easy to move existing files into your workflow. It does not fix Exchange Online or PowerShell API errors.

Frequently Asked Questions
Does Microsoft Graph have a universal 10 MB attachment download limit?
Do not assume that an HTTP 400 means a universal 10 MB limit. Inspect the response body, attachment type, endpoint, and any limits in your script to identify the cause.
Why does my script process only some messages?
The message list can be paged. After processing the value array, request the returned @odata.nextLink URL and repeat until no next link remains. Also check for hard-coded limits in the script.
How do I keep PowerShell from expanding $value in the attachment URL?
Use a single-quoted URL string or escape the dollar sign when constructing the /attachments/{attachment-id}/$value endpoint. Then pass the URL to Invoke-RestMethod with the authorization header.
Can I use the /me endpoint with an app-only token?
No. The /me endpoint requires a signed-in user and delegated authentication. For app-only access, use /users/{id} with the appropriate Microsoft Graph application permission and consent.




