What It Does
Risk is High, so the script only lists the guests it would change unless you set DryRun=false and Approve=remove-stale-guests. It checks for pending invitations older than StaleDays. If sign-in activity is available, it also selects guests with old sign-ins or old accounts with no sign-in on record. If activity cannot be read, it warns and acts on old pending invitations only.
Mode Disable, the default, sets each guest's account to disabled. The guest cannot sign in, nothing is lost, and enabling the account restores access. Guests already disabled are left alone, so a second run changes nothing. Mode Delete deletes the guest account; Microsoft keeps a deleted user for 30 days, when it can still be restored. Use Delete after a Disable run has sat for a while with nobody complaining.
Before the first change it writes the guests to a JSON file and names it. After the changes it reads each guest back, up to six times five seconds apart, and fails if one still shows unchanged. It never changes group memberships or shared files, and MaxChanges (default 25) caps a run.
Partly tested: it ran against a test tenant in both modes and found nothing old enough to change, and its disable and read-back steps are the ones the tested account scripts use. Start with a dry run.
Before You Run It
- Create an app registration in the client tenant with the Graph application permissions User.Read.All and AuditLog.Read.All, plus User.EnableDisableAccount.All for Disable or User.ReadWrite.All for Delete, and give admin consent.
- Run get-stale-guests first, and put partner accounts you must keep in ExcludeGuests.
- Tell the client before deleting: a deleted guest loses access to everything shared with them.
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-stale-guests, 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: Disables, or deletes, guest accounts that are idle or never redeemed.
- Restart: It never restarts the machine.
- Typical run: Under 2 minutes.
- Example answer: "Disabled 8 stale guest(s) (idle or never redeemed for 90 days)"
- Partly tested before listing: run with Windows PowerShell 5.1 against a Microsoft 365 test tenant as far as it allowed, and compared with tested scripts. The help says which part is unproven.
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 Application (client) ID of the app registration that holds the Graph permissions. Required.
- CertificateThumbprint: Thumbprint of the app's certificate (private key in LocalMachine\My or CurrentUser\My). Preferred. When empty, the app secret is read from the environment variable ClientSecret (a secure/hidden RMM field; never an argument).
- StaleDays: A guest with no sign-in, or an unaccepted invitation, for this many days is stale. Whole number, 30 to 3650. Default 90.
- Mode: Disable (default) blocks the guests' sign-in; Delete removes the accounts (restorable for 30 days).
- ExcludeGuests: Guests to leave alone, by sign-in name or mail address, 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.