The Invoke-MgGraphRequest
cmdlet in the Microsoft Graph PowerShell module is a powerful and versatile tool that allows administrators to perform custom API calls to Microsoft Graph. While specific cmdlets like Get-MgUser
exist to fetch Microsoft 365 user details, Invoke-MgGraphRequest
provides greater flexibility for scenarios where precise control or advanced filtering is required.
Here is the syntax for using Invoke-MgGraphRequest to fetch Microsoft 365 users:
Invoke-MgGraphRequest -Method GET -Uri 'https://graph.microsoft.com/v1.0/users'
This example demonstrates how to fetch all users from the Microsoft 365 tenant and loop through the results to display specific details.
$response = Invoke-MgGraphRequest -Method GET -Uri 'https://graph.microsoft.com/v1.0/users'
if ($response.value) {
foreach ($user in $response.value) {
Write-Output "User Details:"
Write-Output "Display Name: $($user.displayName)"
Write-Output "Email: $($user.mail)"
Write-Output "User Principal Name: $($user.userPrincipalName)"
Write-Output "`n"
}
} else {
Write-Output "No users found or the response does not contain a 'value' property."
}
This example fetches details of a single user based on their User Principal Name (UPN):
$upn = "johndoe@contoso.com"
$response = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$upn"
if ($response) {
Write-Output "User Details:"
Write-Output "Display Name: $($response.displayName)"
Write-Output "Email: $($response.mail)"
Write-Output "User Principal Name: $($response.userPrincipalName)"
} else {
Write-Output "No user found with UPN $upn."
}
Invoke-MgGraphRequest -Method GET -Uri 'https://graph.microsoft.com/v1.0/users?$select=displayName,mail,userPrincipalName'
$uri = 'https://graph.microsoft.com/v1.0/users'
do {
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
if ($response.value) {
foreach ($user in $response.value) {
Write-Output $user.displayName
}
}
$uri = $response."@odata.nextLink"
} while ($uri)
try-catch
blocks to handle potential errors effectively.Error | Cause | Solution |
Authentication Failed | The user is not authenticated or lacks the necessary permissions. | Ensure you are authenticated using Connect-MgGraph with the required permissions:
|
403 Forbidden | Insufficient permissions or API access restrictions. | Verify the account permissions in Azure AD and enable API access if required. |
Request_ResourceNotFound | The requested user does not exist. | Double-check the user identifier (UPN or ID) in the URI. |
Response Pagination Issue | Large datasets may cause partial responses. | Implement pagination using the @odata.nextLink property. |
The Invoke-MgGraphRequest
cmdlet is a robust and flexible tool that complements standard Graph PowerShell cmdlets. By allowing custom API requests, it supports advanced automation and integration scenarios, making it indispensable for Microsoft 365 administrators. While it requires more effort compared to purpose-built cmdlets, its flexibility makes it a valuable addition to your administrative toolkit.
© m365corner.com. All Rights Reserved. Design by HTML Codex