What It Does
The script reads one client tenant and writes one row per finding: Disabled (an account with sign-in blocked that still holds licenses), Inactive (an enabled user with licenses who last signed in InactiveDays or more ago), NeverSignedIn (an enabled user with licenses, created at least InactiveDays ago, with no recorded sign-in) and UnassignedSeats (for enabled user plans with seats left, the enabled seats minus the seats assigned). It changes nothing.
Each user row names the licenses and marks the ones assigned through a group, because those cannot be removed from the user directly. Review each Disabled row before reclaiming: a shared mailbox, a mailbox over 50 GB or one on hold is a disabled account that may need its license. Free and trial plans can show very large unassigned counts.
The sign-in check needs Entra ID P1 or P2 and the AuditLog.Read.All permission. Without them the run ends in an error that says so. Set InactiveDays=0 to skip the inactivity check and report only disabled users and unassigned seats, which needs no P1. Microsoft does not return sign-ins from before April 2020.
It keeps no state, so a second run reads the tenant again. When you are ready to reclaim, run remove-unused-m365-licenses, which starts as a dry run.
Before You Run It
- Create an app registration with the Graph application permissions User.Read.All and LicenseAssignment.Read.All, plus AuditLog.Read.All unless you set InactiveDays=0. Give admin consent.
- Check the tenant has Entra ID P1 or P2 if you want the inactivity check.
- Put the app's certificate on the computer that runs the script and use its thumbprint.
- Add shared mailboxes and other accounts that need a license to ExcludeUsers.
- Read every Disabled row before anyone reclaims a license.
Type and Risk
- Type: Report. It collects facts into a table and never alerts. It exits 0, or 2 when it could not run.
- Risk: Low. It reads and changes nothing.
- Restart: It never restarts the machine.
- Typical run: Under 3 minutes.
- Example answer: "Licences: 4 disabled and 7 inactive users hold licences; 12 unassigned seats in 3 SKUs"
- Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- TenantId: The client tenant: its ID (a GUID) or a domain such as contoso.onmicrosoft.com. Required.
- ClientId: The app registration's Application (client) ID, a GUID. Required.
- CertificateThumbprint: Thumbprint (40 hex characters) of the app's certificate with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. When empty, the secret is read from the environment variable ClientSecret (a secure RMM field).
- InactiveDays: An enabled user counts as inactive after this many days without a sign-in. Whole number, 0 to 3650. 0 skips the inactivity check (disabled users and unassigned seats only, no P1 needed). Default 90.
- ExcludeUsers: User principal names to skip, separated by comma, semicolon or new line. Default empty.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.