Skip to content
Menu

Reclaim Microsoft 365 Licences From Disabled Users

Removes directly assigned licenses from disabled users, saves a before-state first, never touches group licenses. Needs DryRun=false and Approve=remove-unused-m365-licenses.

Reclaim Microsoft 365 licences from disabled users is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Reclaim Microsoft 365 licences from disabled users. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, 11, Server 2016+, with the app certificate; runs as SYSTEM or a user
NeedsPowerShell 5.1; Graph: User.Read.All, LicenseAssignment.ReadWrite.All, AuditLog.Read.All; Entra ID P1
Changes thingsChanges what it runs on

What It Does

This script is a High-risk Action. For one client tenant it finds users who hold licenses and are disabled, and removes the licenses assigned directly to them, one change per user. With IncludeInactive=true it also removes the licenses of enabled users with no sign-in for InactiveDays or more, which can take a working person's mailbox and apps away, so it is off by default.

It is a dry run until DryRun=false and Approve=remove-unused-m365-licenses are both set. The dry run lists every name and change and makes none. Read it first. It refuses to remove licenses when more users are planned than MaxChanges (default 25). On a real run it can save the before-state file before refusing the removals.

Before the first change it writes the licenses of every user it will touch, with the plan IDs, names and disabled plans, as JSON under the MSP Outpost data folder and names the file in the output, so a license can be assigned again. After the changes it reads each user back, up to six times, because Entra shows a removal a few seconds late.

It never deletes a user, a mailbox or a subscription, and never changes a license assigned through a group: users with only group licenses are listed as skipped. If both kinds are assigned, it removes direct licenses and warns that group licenses were left alone. A removed license starts Microsoft's grace period for the mailbox and OneDrive. A second run finds nothing left to reclaim and reports it is already clean.

Before You Run It

  • Run get-unused-m365-licenses first and read what it finds.
  • Create an app registration with User.Read.All and LicenseAssignment.ReadWrite.All, plus AuditLog.Read.All for IncludeInactive, with admin consent. Inactive users also need Entra ID P1 or P2.
  • Put every disabled account that needs its license in ExcludeUsers: shared mailboxes over 50 GB or with an archive, and mailboxes on hold.
  • Run the dry run and read every name. Raise MaxChanges only if you mean it.
  • Run it for real with DryRun=false and Approve=remove-unused-m365-licenses, and keep the before-state file.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: High. It is a dry run unless you set both DryRun=false and Approve=remove-unused-m365-licenses, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
  • What it changes: Removes directly assigned licences from disabled users (and inactive users with IncludeInactive); writes a before-state JSON file.
  • Restart: It never restarts the machine.
  • Typical run: Under 5 minutes.
  • Example answer: "Removed licences from 6 disabled user(s)"
  • 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).
  • IncludeInactive: true to also remove the licences of enabled users who have not signed in for InactiveDays. Default false (disabled users only).
  • InactiveDays: With IncludeInactive: days without a sign-in. Whole number, 1 to 3650. Default 90.
  • ExcludeUsers: User principal names never to touch, 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.

The Script

remove-unused-m365-licenses.ps1 · 1074 lines · 62.1 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.