grahamco
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This will be my way to do it.
Tested working on WS2019
$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
Tested working on WS2019
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.
Both Groups Must be given at the 1st 2 lines. So using that logic the groups must be created previously.
ASKER
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.
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.