Skip to content

Repository files navigation

MSGraphRequest

PowerShell Gallery VersionPowerShell 5.1+LicensePowerShell Gallery

A PowerShell module that simplifies interacting with Microsoft Graph API. Provides consolidated authentication (six flows) and functions to perform GET, POST, PATCH, PUT, and DELETE operations with automatic paging, throttling, and token refresh.

v2.0.1 | 100% native REST - zero SDK or MSAL dependencies.

Functions

FunctionDescription
Connect-MSGraphRequestAuthenticate to Microsoft Graph (six flows)
Disconnect-MSGraphRequestClear all tokens and connection state
Invoke-MSGraphOperationExecute Graph API requests with automatic paging, throttling, and token refresh
Test-AccessTokenCheck if the current token is near expiry
Show-AuthenticationInfoDisplay current connection context and token details
Add-AuthenticationHeaderItemAdd custom HTTP headers (e.g. consistencylevel)
Remove-AuthenticationHeaderItemRemove custom HTTP headers

Backward compatibility:Get-AccessToken is available as an alias for Connect-MSGraphRequest.

Installing the module

Install-Module-Name "MSGraphRequest"-AcceptLicense

Requirements

  • PowerShell 5.1 or later
  • No external module dependencies

Supported authentication flows

FlowParameterUse case
Interactive (Auth Code + PKCE)-TenantIdUser sign-in via browser — default flow
Device Code-TenantId -DeviceCodeEnvironments without a browser
Client Secret-TenantId -ClientId -ClientSecretApp-only with a secret
Client Certificate-TenantId -ClientId -ClientCertificateApp-only with a certificate (JWT assertion)
Managed Identity-ManagedIdentityAzure VMs, App Service, Functions
Bring Your Own Token-AccessTokenPre-acquired token from any source

How to use the module

Connect — Interactive (default)

Uses the well-known Microsoft Graph PowerShell app ID (14d82eec-204b-4c2f-b7e8-296a70dab67e) by default. Opens the browser with an account picker.

Connect-MSGraphRequest-TenantId "contoso.onmicrosoft.com"

With a custom app registration:

Connect-MSGraphRequest-TenantId "contoso.onmicrosoft.com"-ClientId "00000000-0000-0000-0000-000000000001"

Connect — Device Code

Connect-MSGraphRequest-TenantId "contoso.onmicrosoft.com"-DeviceCode

Connect — Client Secret

Connect-MSGraphRequest-TenantId "contoso.onmicrosoft.com"-ClientId "<AppId>"-ClientSecret "<Secret>"

Connect — Client Certificate

$cert=Get-Item"Cert:\CurrentUser\My\<Thumbprint>"Connect-MSGraphRequest-TenantId "contoso.onmicrosoft.com"-ClientId "<AppId>"-ClientCertificate $cert

Connect — Managed Identity

# System-assignedConnect-MSGraphRequest-ManagedIdentity
# User-assignedConnect-MSGraphRequest-ManagedIdentity -ManagedIdentityClientId "<ClientId>"

Connect — Bring Your Own Token

Connect-MSGraphRequest-AccessToken $myToken

Graph API requests

Invoke-MSGraphOperation handles paging (@odata.nextLink), throttling (Retry-After), and automatic token refresh.

# GET all Intune managed devicesInvoke-MSGraphOperation-Get -Resource "deviceManagement/managedDevices"-APIVersion "v1.0"# GET with Beta APIInvoke-MSGraphOperation-Get -Resource "devices"-APIVersion "Beta"# POSTInvoke-MSGraphOperation-Post -Resource "deviceManagement/deviceCategories"-Body ($body|ConvertTo-Json) -APIVersion "v1.0"# PATCHInvoke-MSGraphOperation-Patch -Resource "deviceManagement/managedDevices('$id')"-Body ($body|ConvertTo-Json)
# DELETEInvoke-MSGraphOperation-Delete -Resource "deviceManagement/managedDevices('$id')"

Custom headers

Some Graph operations require additional headers (e.g. advanced queries with $count):

Add-AuthenticationHeaderItem-Name "consistencylevel"-Value "eventual"# Later, remove if no longer neededRemove-AuthenticationHeaderItem-Name "consistencylevel"

Custom headers are preserved across automatic token refreshes.

Check connection status

# Quick expiry checkTest-AccessToken# Full connection contextShow-AuthenticationInfo# Decoded JWT payloadShow-AuthenticationInfo-FullDetails

Disconnect

Disconnect-MSGraphRequest

About

No description or website provided.

Topics

Resources

Stars

16 stars

Watchers

4 watching

Forks

Contributors

Languages