Skip to content
Menu

Employee Left - Microsoft 365 Offboarding

Runs the leaver steps for one user, each logged and each switchable, and saves the groups, licenses and mailbox settings first. It deletes nothing, and a dry run lists every step.

Employee left - Microsoft 365 offboarding 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 Employee left - Microsoft 365 offboarding. 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, group and license permissions (see the script); Exchange app access for mail
Changes thingsChanges what it runs on

What It Does

This script changes things, for the one user in UserPrincipalName. Each step is a separate logged change and each is switched off by its input: block sign-in, revoke sessions, remove from groups, convert the mailbox to shared, set the automatic reply (AutoReplyText), give ManagerUpn forwarding with a copy kept or Full Access or both (ManagerMailboxAccess), hide the mailbox from address lists, and remove the licenses assigned directly to the user. The steps are on by default, except the automatic reply and the manager steps, which need AutoReplyText and ManagerUpn.

Group removal runs before mailbox conversion and can remove licenses inherited from those groups. If conversion is enabled and the user has a mailbox to convert, conversion runs before direct license removal. A failing mailbox step stops the run before direct licenses are removed. Before anything changes, the user's groups, licenses and mailbox settings are saved as JSON under the MSPOutpost data folder, and the file is named in the output, so you can review and restore settings where supported.

Groups that are dynamic, synced from on-premises Active Directory, role-assignable, or mail-enabled security or distribution groups are skipped and listed. Licenses that come from a group go when the membership goes, and are listed otherwise. A user synced from on-premises Active Directory cannot be blocked or hidden from Microsoft 365: those two steps are skipped with a note and must be done in Active Directory.

It never deletes the user, the mailbox, a group or a license subscription, never resets the password and never removes OneDrive data. It is a dry run until DryRun=false and Approve=invoke-m365-offboarding are both set, and it refuses to make more than MaxChanges changes in one run (default 25).

The mailbox steps need the ExchangeOnlineManagement module on this computer: if it is missing, the run ends SKIPPED before anything changes. Running it again skips what is already done where it can tell, such as a blocked account, a shared mailbox and licenses already removed, and says already offboarded when nothing is left. Revoking sessions can be repeated.

Before You Run It

  • Create an app registration with the Graph permissions in the header and grant admin consent. If the user is an administrator, the app also needs an Entra role that may change administrators.
  • For the mailbox steps, install ExchangeOnlineManagement 3.1 or later and give the app Exchange.ManageAsApp and the Exchange Administrator role.
  • Decide who gets the mail: set ManagerUpn and ManagerMailboxAccess, and write AutoReplyText.
  • Confirm the person has really left before you approve it.
  • Run a dry run first and read every step, the skipped groups and the licenses.
  • For a user synced from Active Directory, block sign-in and hide the mailbox there. Check which group licenses will go before the mailbox conversion.

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=invoke-m365-offboarding, 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: Blocks sign-in; revokes sessions; removes group memberships; converts the mailbox to shared; sets the auto-reply, forwarding, manager access and address-list visibility; removes direct licences; writes a before-state JSON file.
  • Restart: It never restarts the machine.
  • Typical run: Under 3 minutes.
  • Example answer: "Offboarded [user sign-in]: sign-in blocked, sessions revoked, 4 group(s), mailbox shared, 2 licence(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).
  • UserPrincipalName: The user to offboard, such as [user sign-in]. Required. One user per run.
  • BlockSignIn: true to block sign-in. Default true.
  • RevokeSessions: true to revoke sessions and refresh tokens. Default true.
  • RemoveFromGroups: true to remove the user from groups (not dynamic, not synced, not role-assignable, not mail-enabled security or distribution). Default true.
  • ConvertToShared: true to convert the mailbox to a shared mailbox. Default true.
  • AutoReplyText: Text of the automatic reply for internal and external senders. Empty (default) leaves the auto-reply alone.
  • ManagerUpn: The manager who receives the mail or the access, such as [manager sign-in]. Empty (default) skips that step.
  • ManagerMailboxAccess: What ManagerUpn gets: FullAccess (default), Forward (forward and keep a copy), Both or None.
  • HideFromAddressLists: true to hide the mailbox from address lists. Default true.
  • RemoveLicenses: true to remove the directly assigned licences, after the mailbox steps. Default true.

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

invoke-m365-offboarding.ps1 · 1235 lines · 73.8 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.