What It Does
For the one group in GroupName the script can create it (a security group of scope GroupScope in GroupOu, only when CreateGroup is true and it does not exist), add every name in AddMembers that is not already a direct member, and remove every name in RemoveMembers that is. Names are sign-in names of users, groups or computers, separated by a comma, semicolon or new line. Only direct membership changes.
Every name is looked up before anything is changed. A name that is not found, matches more than one object, is in both lists or is the group itself ends the run in ERROR with nothing changed. A missing group is also an error unless CreateGroup is true. The change limit is checked before the first change.
Before changing an existing group it saves the current direct members as a JSON list under the MSP Outpost Backups folder, so the old membership can be restored. It never touches privileged groups: built-in groups, Domain Admins, Enterprise Admins, Schema Admins, Domain Controllers, Group Policy Creator Owners, read-only domain controllers, Key Admins, or any group with adminCount 1. Use Active Directory Users and Computers for those.
When the group exists, already has every member to add and none to remove, the run ends OK with Already as wanted and changes nothing. DryRun lists the steps and makes none. After a real run it reads the membership back and fails loudly if a member is missing or still there. To only list members, use get-ad-group-members.
Before You Run It
- Run it on a domain controller or a member server with the ActiveDirectory module (RSAT) and rights to change group membership.
- Run it with DryRun=true first and read the lines saying who would be added and removed.
- Use sign-in names, not display names. A name that matches two objects is refused.
- Remember that only direct members change. A person in a nested group is not added or removed.
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: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Optionally creates one security group, adds and removes direct members (before-state saved).
- Restart: It never restarts the machine.
- Typical run: Under 15 seconds.
- Example answer: "Group Sales-Staff: added 2 member(s), removed 1 member(s)"
- Tested before listing on a Windows Server 2025 domain controller (VM) with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
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.
- GroupName: The group's sign-in name (sAMAccountName). Required. Up to 63 letters, digits, spaces, dots, dashes or underscores.
- AddMembers: Names to add. Default empty.
- RemoveMembers: Names to remove. Default empty.
- CreateGroup: true to create the group when it does not exist. Default false: a missing group is an error.
- GroupScope: The scope of a group this run creates: Global (default), DomainLocal or Universal. Ignored when the group exists.
- GroupOu: Distinguished name of the OU or container for a group this run creates. Default empty: the domain's Users container.
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.