When working with Microsoft Graph API through PowerShell, especially in environments with high volumes of requests, you might encounter throttling limits.
Throttling happens when too many requests are sent to the API within a short time frame, resulting in a Too Many Requests response or Too many retries performed error. This is Microsoft’s way of managing the load on their servers and ensuring fair usage across all clients.
To handle throttling effectively, it’s important to implement retry logic in your PowerShell scripts using the Graph SDK.
This blog post will guide you through a simple approach to managing throttling in your PowerShell scripts.
We recently encountered an issue while attempting to retrieve over 100,000 records using the Microsoft Graph API with the PowerShell Graph SDK.
The API started failing after processing about 15 pages, returning the following error:
Invoke-MgGraphRequest -Method Get -Uri $apiUrl
$apiUrl = ‘https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations/’ + $policyId + ‘/deviceStatuses’

After conducting some research, we found a solution to this problem. By adding the following command after establishing the connection to Microsoft Graph, we can mitigate the throttling issues:
Connect-MgGraph -ClientId $MgGClientID -CertificateThumbprint $ThumbPrint -TenantId $TenantName -NoWelcome # connection command
To avoid throttling, include the following command to set the request context:
Set-MgRequestContext -MaxRetry 5 -RetryDelay 10
This configuration ensures that our requests are automatically retried up to five times with a delay of ten seconds between each attempt, significantly reducing the likelihood of encountering throttling errors.
Thanks for reading …
Tech Wizard