Lab · Intermediate · 30 min
Run Microsoft Graph PowerShell unattended with a certificate
Scheduled reports should not run with a person's account. This lab creates an app registration that signs in with a certificate and has only the application permissions the report needs.
What you need
- A test tenant
- Windows with PowerShell 7.4
- Microsoft.Graph module
Steps
- Create a self-signed certificate
For a lab a self-signed certificate is fine; production should use one from your PKI.
PowerShell $cert = New-SelfSignedCertificate -Subject "CN=GraphReports" -CertStoreLocation Cert:\CurrentUser\My -KeySpec Signature -NotAfter (Get-Date).AddYears(1) Export-Certificate -Cert $cert -FilePath .\GraphReports.cer - Register the app
In Entra admin center, App registrations, New registration. Upload GraphReports.cer under Certificates & secrets.
- Grant application permissions
Add Microsoft Graph application permissions such as User.Read.All and Reports.Read.All, then Grant admin consent.
- Connect without a user
Use the tenant ID, client ID and certificate thumbprint.
PowerShell Connect-MgGraph -TenantId <tenant-id> -ClientId <client-id> -CertificateThumbprint $cert.Thumbprint -NoWelcome Get-MgContext | Select-Object AuthType, Scopes - Run a report
Any read-only sample works, for example the licence report.
PowerShell Get-MgSubscribedSku | Select-Object SkuPartNumber, ConsumedUnits
Check your result
Get-MgContext shows AuthType AppOnly and the report returns data.
InnovaticT