Attachment Issues

Fix PowerShell Invoke-RestMethod Errors for Large Exchange Online Attachments

Amos GikundaAmos Gikunda Nov 25, 2026 868 views

Question details

The user needs to download large Exchange Online email attachments and process all available messages with PowerShell.

Fix PowerShell Invoke-RestMethod Errors for Large Exchange Online Attachments
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.
Before you start

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.

Solution 1Recommended

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.

1
Authorize mailbox access

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.

2
List and page through messages

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.

3
Find the attachment

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.

4
Download the file content

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.

5
Check the results

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.

Download file attachments through Microsoft Graph and follow message pages
Attachment types differ: The raw-content endpoint works differently for item attachments, while reference attachments do not provide file content through $value. Check the attachment type before choosing a download method.
Free Microsoft Office alternative

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.

Open and edit supported documents downloaded from email attachmentsCompatible with common Microsoft Office formats, including DOCX, XLSX, and PPTXFamiliar interface for a straightforward transitionFree and lightweight for everyday document work
microsoft office alternative - wps office

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.