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

  1. 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
  2. Register the app

    In Entra admin center, App registrations, New registration. Upload GraphReports.cer under Certificates & secrets.

  3. Grant application permissions

    Add Microsoft Graph application permissions such as User.Read.All and Reports.Read.All, then Grant admin consent.

  4. 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
  5. 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.

Microsoft documentation

Disclaimer. These samples are provided as is, without warranty of any kind. Read and test every script in a non-production tenant before you run it, and run reports before any command that changes or deletes data.