Script repository

Remove all members from a group and add to another group

Updated on: Jan 18, 2026, Views: 2434

Group membership

The script removes all members from the target group and adds them to the specified group. To execute the script, create a business rule, custom command or scheduled task configured for the Group object type.

In the script, the $targetGroupDN variable specifies the distinguished name (DN) of the group to which members will be added. For information on how to get the DN, see Get the DN of a directory object.

$targetGroupDN = "CN=MyGroup,OU=Groups,DC=domain,DC=com" # TODO: modify me

# Get source group members.
$sourceGroup = $Context.TargetObject
try
{
    $memberGuidsBytes = $sourceGroup.GetEx("adm-DirectMembersGuid") 
}
catch
{
    return
}

$targetGroup = $Context.BindToObjectByDN($targetGroupDN)
foreach ($guidBytes in $memberGuidsBytes)
{
    # Remove member from the source group.
    $guid = [Guid]$guidBytes
    $sourceGroup.Remove("Adaxes://<GUID=$guid>")
    
    if ($targetGroup.IsMember("Adaxes://<GUID=$guid>"))
    {
        continue
    }
    
    # Add member to the target group.
    $targetGroup.Add("Adaxes://<GUID=$guid>")
}

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.