/

How to Run Scheduled Graph API Checks for Expired Licenses and Inactive Users

by Mauro Mendoza, IT Technical Writer
How to Run Scheduled Graph API Checks for Expired Licenses and Inactive Users blog banner image
How to Run Scheduled Graph API Checks for Expired Licenses and Inactive Users blog banner image

Try our #1 rated endpoint management platform on G2

NinjaOne dashboard screenshot

Automate the Hardest Parts of IT

Key Points

  • Detect risk: Query the Microsoft Graph API with PowerShell to identify expired Microsoft 365 licenses and Azure AD accounts that have been inactive for 90+ days.
  • Require the right access: Set up a Global Admin or Reports Reader role, the Microsoft Graph PowerShell SDK, and User.Read.All, LicenseAssignment.ReadWrite.All, and AuditLog.Read.All permissions before running the checks.
  • Export and archive results: Save findings to timestamped CSV reports to maintain an audit trail for compliance and license reclamation decisions.
  • Automate the schedule: Run the script on a recurring basis using Windows Task Scheduler on a single machine, or push it to multiple endpoints and tenants via an RMM tool.
  • Exclude and calibrate: Whitelist service accounts and break-glass accounts, and set inactivity thresholds that match your organization’s risk tolerance.

Unused Microsoft 365 licenses are a silent budget drain, while inactive Azure AD accounts pose a real security risk. Automate checks using the Microsoft Graph API to detect expired licenses and dormant accounts before they lead to wasted spend or security gaps.

In this guide, we will help you schedule critical checks via PowerShell to lock down vulnerabilities and scale across clients.

For a quick visual overview, watch our video guide on How to Run Scheduled Graph API Checks for Expired Licenses and Inactive Users.

Step-by-step procedure to schedule Graph API checks via PowerShell

Automate Graph API checks to reclaim wasted licenses, secure dormant accounts, meet compliance, and streamline tenant management—done in one sweep.

Use case: Do this method during monthly cost reviews, security audits, or before license renewals. This is ideal for MSPs reclaiming expired Office 365 licenses or hardening security after employee offboarding. This can help spot inactive users before they become vulnerabilities to your system.

Prerequisites: Before we proceed with the procedure, ensure you have the following:

  • Administrative permissions: Make sure your account has a Global Admin or Reports Reader role in Azure AD.
  • Tools: Microsoft Graph PowerShell SDK (Microsoft.Graph module) and PowerShell 7+.
  • Azure setup: Ensure your Azure AD app has the User permission.Read.All, LicenseAssignment.ReadWrite.All, and AuditLog.Read.All permissions for automation.
  • Optional but recommended: Download and install an RMM tool like NinjaOne to schedule scripts, monitor outputs, and scale across tenants.

Step 1: Install and authenticate Microsoft Graph SDK

Set up your toolkit to automate license and user checks. Installing the Graph SDK to query Azure AD data (which the admin usually hides) authenticates ties to your script to Microsoft’s API.

Step-by-step procedure:

  1. Install the SDK by running this script in PowerShell (Admin) or Windows Terminal (Admin):

Install-Module Microsoft.Graph -Scope CurrentUser -Force

  1. Authenticate using this script:

Connect-MgGraph -Scopes "User.Read.All", "LicenseAssignment.ReadWrite.All", "AuditLog.Read.All

You can also configure a registered app with a client secret and use certificate or client credentials flow for unattended execution.

After you’ve authenticated Graph SDK, you can query inactive Azure AD users and expired Office 365 licenses in the next steps.

Step 2: Query for users with expired or unassigned licenses

This step will help uncover expired licenses and inactive accounts that pose a security risk and unnecessary costs.

Step-by-step procedure:

  1. In PowerShell (Admin), run this script to check for unassigned licenses at the tenant level:

Get-MgSubscribedSku |
Select-Object SkuPartNumber,
@{Name="TotalLicenses"; Expression={$_.PrepaidUnits.Enabled}},
@{Name="UsedLicenses"; Expression={$_.ConsumedUnits}},
@{Name="Available"; Expression={$_.PrepaidUnits.Enabled - $_.ConsumedUnits}} |
Format-Table -AutoSize

  1. Run this script to find unlicensed users:

Get-MgUser -Filter "assignedLicenses/`$count eq 0" `
-ConsistencyLevel eventual `
-CountVariable unlicensedCount `
-All |
Select-Object DisplayName, UserPrincipalName |
Export-Csv -Path "UnlicensedUsers.csv" -NoTypeInformation

  1. Run this script to find inactive users with licenses:

$daysInactive = 90
$cutoffDate = (Get-Date).AddDays(-$daysInactive)# Get licensed inactive users
Get-MgUser -All -Property "DisplayName,UserPrincipalName,SignInActivity,AssignedLicenses" |
Where-Object {
($_.SignInActivity.LastSignInDateTime -lt $cutoffDate) -and
($_.AssignedLicenses.Count -gt 0)
} |
Select-Object DisplayName,
UserPrincipalName,
@{Name="LastSignIn"; Expression={$_.SignInActivity.LastSignInDateTime}} |
Export-Csv -Path "InactiveLicensedUsers.csv" -NoTypeInformation

Important: Keep in mind that SignInActivity requires Azure AD Premium P1/P2. Avoid beta endpoints for production scripts as well.

After running these scripts, you’ll have two CSV files: UnlicensedUsers.csv, which contains users that need license assignment; and InactiveLicensedUsers.csv, which lists accounts that are security risks.

Step 3: Export and log audit results

After getting the list of expired and unassigned licenses, turn this data into actionable insights with automated reporting. Exporting this data creates permanent audit trails as proof of compliance, and automation ensures you catch issues before they become costly.

Step-by-step procedure:

  1. In PowerShell (Admin), run this script to export to Timestamped CSV:
    • Script to generate a filename with a timestamp:

$timestamp = Get-Date -Format "yyyyMMdd-HHmm"
$reportPath = "C:\Audits\LicenseReport_$timestamp.csv"

    • Export inactive users with licenses:

$inactiveUsers | Export-Csv -Path $reportPath -NoTypeInformation

    • This will create universally openable CSV files, and the timestamping will create versioned records for compliance.
  1. (Optional): Run this script to email reports:

Send-MailMessage -From "[email protected]" -To "[email protected]" `
-Subject "ACTION REQUIRED: $($inactiveUsers.Count) Inactive Licensed Users" `
-Body "Attached: Users with licenses but no sign-ins for 90+ days" `
-Attachments $reportPath -SmtpServer "smtp.office365.com" -Port 587 `
-Credential (Get-Credential)

  1. Run this script to fully automate with the Azure AD app:

$tenantId = "YOUR_TENANT_ID"
$clientId = "YOUR_CLIENT_ID"
$clientSecret = "YOUR_SECRET" | ConvertTo-SecureString -AsPlainText -ForceConnect-MgGraph -TenantId $tenantId -ClientId $clientId -ClientSecret $clientSecret

Tip: Store secrets in Azure Key Vault. Never hardcode them.

After running these scripts, you’ll have timestamped CSV reports stored locally and optional email alerts to stakeholders instantly. These files can help you make automated runs via Task Scheduler or RMM tools like NinjaOne to ensure continuous monitoring.

Step 4: Automate with Task Scheduler or RMM policy

Schedule your script once and let it run monthly, yearly, or forever, ensuring proactive monitoring without lifting a finger. Regular check-ups help catch issues before they cause problems. There are two options to do this:

Option 1: Via Task Scheduler (Single Machine)

Step-by-step procedure:

  1. Open Command Prompt (Admin), then run this command:

schtasks /create /tn "M365LicenseAudit" /tr `
"powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\LicenseAudit.ps1" `
/sc weekly /d MON /st 02:00 /ru "NT AUTHORITY\SYSTEM"

    • This works because it runs as a system account even when no user is logged in.

Option 2: Via RMM tools

Step-by-step procedure:

  1. Open your preferred RMM tool such as NinjaOne.
  2. Create the script (from Option 1), then store it in NinjaOne’s script library.
  3. Assign the script to the Microsoft 365 Clients policy group.
  4. Configure outputs:
    • Save CSVs to \\ClientShare\Audits.
    • Upload to the RMM’s dashboard for centralized viewing.

After scheduling the script, it will run on the assigned date and time, auto-saving the reports to designated folders, and showing the trends among tenants.

Things to look out for

This section highlights potential challenges to keep in mind as you follow this guide.

RisksPotential ConsequencesReversals
1. Missing data in reportsIncomplete audits, undetected risksCheck permissions: Run Get-MgContext to verify scopes like User.Read.All.

Bypass MFA blocks: Use certificate auth instead of secrets.

2. Script fails silentlyFalse sense of security, compliance gapsEnable logging: Add Start-Transcript -Path C:\Logs\Audit.log to scripts.

Monitor Event Viewer: Check under Applications and Services Logs > PowerShell.

3. Users not flaggedExpired licenses/inactive accounts missedValidate thresholds: Confirm that the $cutoffDate aligns with the policy.

Verify license fields: Use Get-MgUser -UserId [email protected] -Property AssignedLicenses.

4. Hardcoded secretsTenant compromise, credential theftRotate secrets immediately in Azure AD.

Migrate to Azure Key Vault: Use Get-AzKeyVaultSecret to securely retrieve secrets.

5. Beta API dependencyScript breaks after Microsoft updatesReplace beta endpoints: Use Get-MgUser (v1.0) instead of Get-MgBetaUser.

Subscribe to the Microsoft Graph changelog.

Key considerations for bulletproof Graph API PowerShell audits

Tweak these settings to match your environment. Ensuring precision prevents false alarms and keeps workflows smooth.

Report exclusions

Exclude service accounts, shared mailboxes, and break-glass accounts from inactivity scans. This prevents accidental license reclamation for critical resources. You can use PowerShell filters to whitelist them, using this script:

$excludedUPNs = @("[email protected]", "[email protected]")
Get-MgUser | Where-Object { $_.UserPrincipalName -notin $excludedUPNs }

Inactivity thresholds

Set thresholds based on your risk tolerance. If an account is inactive for 90 days, it is now low risk, while 60 days is medium risk, and 30 days is high risk. Based on the risk, take action by sending our warning emails, revoking their licenses, or disabling the account, then reclaim. This ensures you are protected from issues.

Auditing frequency

Time your audits properly to avoid security risks and other issues. For most businesses, it is recommended to have monthly checks to align with billing cycles.

In high-turnover industries, conducting weekly checks ensures reports keep pace with environmental changes. You should also trigger the audit immediately upon each employee’s exit from the company.

Action mapping

Build SOPs that auto-trigger next steps. Run the following scripts to reclaim licenses and disable accounts:

  • To reclaim licenses:

Set-MgUserLicense -UserId "[email protected]" -RemoveLicenses @("skuId")

  • To disable accounts:

Update-MgUser -UserId "[email protected]" -AccountEnabled:$false

After running these scripts, archive the data. Forward them to the manager/SharePoint via Graph API.

How NinjaOne can streamline your license audits

Transform Graph API checks from manual tasks into fully automated, cross-tenant compliance engines.

  • Centralized script deployment: Push your Graph API PowerShell scripts to 100+ endpoints/tenants in one action.
  • Automatic output monitoring: Track CSV reports/logs across all clients. Alerts fire instantly if files don’t update.
  • Failure alerts in your tools: Get Slack, Team, or email notifications when scripts fail.
  • Unified cross-tenant reports: Merge expired Office 365 licenses and inactive users’ data into one executive dashboard.
  • Auto-trigger remediation: Automatically create tickets in your PSA tool using predefined SOPs to detect expired licenses.

Lock down costs and security with Graph API license checks

Automating license and user audits transforms reactive oversight into proactive control. By reclaiming unused licenses and disabling dormant accounts, you prevent billing leaks and shut down attack vectors. Use the procedure above to turn unused licenses into budget savings and stale accounts into compliance.

Quick-Start Guide

NinjaOne can help you run scheduled Graph API checks for expired licenses and inactive users. NinjaOne platform offers tools and guidance for:

  • Auditing expired licenses: Use Graph API to identify licenses that have passed their expiration date
  • Checking inactive users: Detect users who haven’t logged in or used services within a defined period
  • Automating these checks: Set up scheduled tasks to run these audits regularly

Related topics

FAQs

You need a Global Admin or Reports Reader role in Azure AD, along with the Microsoft Graph PowerShell SDK and PowerShell 7 or later. Your Azure AD app registration also needs User.Read.All, LicenseAssignment.ReadWrite.All, and AuditLog.Read.All permissions.

Most organizations run these checks monthly to align with billing cycles, while high-turnover industries benefit from weekly checks. Trigger an audit immediately upon an employee’s departure from the company.

Yes. Use a PowerShell filter to whitelist service accounts, shared mailboxes, and break-glass accounts by user principal name so they aren’t flagged or accidentally disabled.

Task Scheduler is suited to a single machine or tenant. An RMM tool like NinjaOne can push the same script across many endpoints and tenants, centralize the output, and alert you if a run fails.

You might also like

Ready to simplify the hardest parts of IT?