Link to home
Start Free TrialLog in
Avatar of grahamco
grahamcoFlag for United States of America

asked on

Move half of group members to new group via Powershell

I have a security group A with 144 total members, i need to move half or 72 members to group B. Is there a way to do this via powershell?
Avatar of arnold
arnold
Flag of United States of America image

See https://docs.microsoft.com/en-us/powershell/module/addsadministration/Add-ADPrincipalGroupMembership?view=win10-ps

Do you have a criteria by which you choose which users move?

The root of this module includes the directive to remove the user from a group.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will be my way to do it.

$GroupA="grouptest"
$GroupB="GroupB"
$members = Get-ADGroupMember -identity $GroupA

#create subgroup with the half of users
$half= $members.Count  /2

$MembersToMove = $members | select -First $half

#now remove them from the initial groupA
Remove-ADGroupMember -Identity $GroupA -Members $MembersToMove -confirm:$false

#And Add it to the new group
Add-ADGroupMember -Identity $GroupB -Members $MembersToMove

Open in new window


Tested working on WS2019
Avatar of grahamco

ASKER

Jose Gabriel Ortega Castro:  im confused by lines 15 if the goal is to remove them from GroupA why are you using the Add-ADGroupMember cmdlet? Also will this script work if the groups already exist. So GroupA currently has ALL 144 members, i want to move 72 of them to GroupB and remove them from GroupA.
Yeah that was a typo, I'm removing the half of people that will be transferred from the groupA to the groupB I just forgot to change the Cmdlet to the Remove-AdGroupmember, but it's all corrected already.

Both Groups Must be given at the 1st 2 lines. So using that logic the groups must be created previously.

Thanks Jose, OBdA suggestion turned out to be the one i used. It's a smaller script but yours appears to work as well. Thank you both i'm sure i'll use pieces of both scripts for future tasks.