What It Does
This script changes things in one way: it adds the new user to groups. It reads the groups TemplateUpn is a direct member of, reads the groups NewUserUpn is already in, and adds the new user to each template group they are missing. It never removes a user from a group, never changes the template user, never creates a group or directly changes its settings, licenses or roles. A license that comes from group based licensing follows the new membership by itself, and a Microsoft 365 group membership is also the user's Team membership.
It skips, and lists with the reason, the groups that cannot or should not be changed from here: dynamic membership groups, groups synced from on-premises Active Directory, role-assignable groups (they grant Entra administrator roles), mail-enabled security and distribution groups (managed in Exchange), and any group named in SkipGroups. TemplateUpn and NewUserUpn must be different accounts.
Group membership can grant access, so it is a dry run that lists the groups it would add until DryRun=false and Approve=invoke-group-membership-copy are both set. One run adds at most MaxChanges memberships (default 25). Afterwards it reads the new user's groups again, up to six times, and ends in an error if any are still missing.
Running it again adds only what is still missing and then says already the same. Create the new user first: the script stops if it cannot read them.
Before You Run It
- Create an app registration with GroupMember.ReadWrite.All and Directory.Read.All, and grant admin consent.
- Create the new user first. The script stops if the account does not exist.
- Pick a template user whose groups are right for the role: a senior colleague may hold access the new starter should not have.
- Run a dry run first, and read the groups it would add and the ones it skips.
- Put groups that grant sensitive access in SkipGroups.
- Do the skipped groups by hand: dynamic groups by rule, synced groups in Active Directory, mail groups in Exchange.
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-group-membership-copy, 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: Adds the new user to the security and Microsoft 365 groups the template user is in.
- Restart: It never restarts the machine.
- Typical run: Under 2 minutes.
- Example answer: "Added [new user sign-in] to 7 group(s) copied from [user sign-in]; 2 skipped"
- 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).
- TemplateUpn: The user whose groups are copied, such as [user sign-in]. Required.
- NewUserUpn: The user who receives the groups, such as [new user sign-in]. Required. Must be a different account from TemplateUpn.
- SkipGroups: Group display names NOT to copy, separated by comma or semicolon (exact name, any case). 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.