What It Does
This is a High-risk Action, so it is a dry run until you set DryRun=false and Approve=disable-shared-mailbox-sign-in. The dry run lists the mailbox accounts it would block and changes nothing. Read that list before you approve.
It finds what test-shared-mailbox-sign-in finds: shared mailboxes and, unless IncludeResources=false, room and equipment mailboxes whose Entra account is enabled. For each one it blocks the account, so only direct sign-in with the mailbox's own password stops. Mail delivery, delegates' access and calendar booking keep working.
It never touches the mailbox, its licenses, its permissions or its data, and deletes nothing. Do not block a mailbox that an application or a person really signs in to, such as a scanner or an old script: list it in ExcludeMailboxes first. Accounts synced from on-premises Active Directory cannot be changed this way: they are listed as skipped and must be disabled in Active Directory.
Before it changes anything it saves each mailbox, its user ID and its account state to a before-state JSON file under the MSPOutpost data folder on this computer, and prints the path. To undo, set the accounts in that file back to enabled. It stops without changing anything when more than MaxChanges accounts are planned, 25 unless you raise it, so check the dry run and then raise MaxChanges.
Afterwards it reads every account back, retrying for a short while because Entra shows a change late, and ends with an error if one is still enabled. A second run after a real change ends OK and says it is already done. If the ExchangeOnlineManagement module is not installed, the run is skipped.
Before You Run It
- Run test-shared-mailbox-sign-in first, and read the list.
- Register an app for this client with a certificate, and keep the certificate with its private key on the computer that runs the script.
- Give the app the Microsoft Graph application permissions Organization.Read.All, User.Read.All and User.EnableDisableAccount.All, and the Exchange.ManageAsApp permission.
- Assign the app an Exchange read role, for example Global Reader. An Entra role may also be needed if an account to block is an administrator.
- List in ExcludeMailboxes every mailbox that something signs in to, such as a scanner.
- Run the dry run, and raise MaxChanges only if the planned count is right.
- Keep the before-state file until you are sure nothing broke.
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=disable-shared-mailbox-sign-in, 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: Sets accountEnabled false on the Entra account of each shared, room or equipment mailbox that can sign in; writes a before-state JSON file.
- Restart: It never restarts the machine.
- Typical run: Under 5 minutes on a large tenant.
- Example answer: "Blocked sign-in on 3 shared/resource mailbox(es); before-state saved"
- 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's tenant ID (a GUID) or domain such as contoso.onmicrosoft.com. Required.
- ClientId: The app registration's Application (client) ID, a GUID. Required.
- CertificateThumbprint: Thumbprint of the app's certificate, with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. Leave empty to use the ClientSecret environment variable.
- IncludeResources: true (default) to handle room and equipment mailboxes as well as shared mailboxes.
- ExcludeMailboxes: Addresses (or UPNs) that must keep sign-in enabled, separated by comma, semicolon or new line. Default none.
- TargetMailbox: Work on one mailbox only (its address or UPN). Default empty: every shared, room and equipment mailbox.
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.