Introduction
Efficient management of Microsoft 365 licenses is essential for reducing costs and optimizing resources. Thanks to PowerShell and Microsoft Graph tools, it is possible to identify inactive user accounts and take action to recover unused licenses.
Why Identify Inactive Accounts?
The increase in Microsoft 365 license costs, scheduled for July 2026, is pushing IT professionals to review the use of assigned licenses. Identifying inactive accounts allows you to:
- Reduce spending related to unused licenses.
- Optimize budgets by replacing expensive licenses with cheaper alternatives.
- Avoid management errors in license assignment (for example, licenses assigned to disabled accounts).
Good to Know
The Microsoft 365 Licensing Report script (version 1.96) can help you identify inactive accounts and analyze license usage in your environment.
Using PowerShell to Find Inactive Accounts
PowerShell, combined with the Microsoft Graph PowerShell SDK, offers a fast and flexible method to identify inactive user accounts. Here is a step-by-step guide:
Connect to Microsoft Graph
Sign in to Microsoft Graph with an administrator account and provide consent for the necessary permissions, including User.Read.All, Places.Read.All, and LicenseAssignment.Read.All.
1Connect-MgGraph -Scopes "User.Read.All", "Places.Read.All", "LicenseAssignment.Read.All"Retrieve Licensed Accounts
Run a PowerShell command to identify accounts with assigned licenses. This includes user accounts and room mailboxes.
1[array]$Users = Get-MgUser -Filter "assignedLicenses/$count ne 0 and userType eq 'Member'" -Property Id, DisplayName, userPrincipalName, AssignedLicenses, SignInSessionsValidFromDateTime -All2[array]$Places = Get-MgPlaceAsRoom -All -PageSize 500Create a License Conversion Table
Generate a mapping table between license SKU identifiers and their names.
1[array]$SKUs = Get-MgSubscribedSKU | Select-Object SkuId, SkuPartNumber2$SKUHash = @{}3ForEach ($SKU in $SKUs) {4 $SKUHash.Add($SKU.SkuId, $SKU.SkuPartNumber)5}Identify Inactive Accounts
Define a cutoff date for inactive accounts (90 days) and identify users who have not signed in recently.
1$CutoffDate = (Get-Date).AddDays(-90).ToUniversalTime()2ForEach ($User in $Users) {3 If (($User.SignInSessionsValidFromDateTime -lt $CutoffDate)) {4 Write-Output "Inactive account: $User.DisplayName"5 }6}Results and Actions
Once inactive accounts are identified, you can:
- Replace expensive licenses with cheaper alternatives.
- Remove licenses from unused accounts.
- Analyze specific needs (example: extended leave or temporary interruptions).

Tip
Use the complete script available on GitHub Office 365 IT Pros to automate the process.
Conclusion
Optimizing the use of Microsoft 365 licenses through PowerShell and Microsoft Graph tools is a strategic approach for IT professionals. By quickly identifying inactive accounts, you can reduce costs and improve the efficiency of your M365 environment.



