locked
Compare and Remove user from a Group A and add to Group B RRS feed

  • Question

  • I have a list of groups in a CSV file where there are 10 users in the Group A

    I need to compare the users in Group B with Group A and remove the users who are part of Group A and add them to Group B. 

    Example:

    Group A (Primary Group)      Group B(secondary group)

    XXX                                       xxx

    yyy            

    zzz                                         zzz

    we are trying to eliminate multilevel hierarchy of viewing the users 

    output will be like this format:

    Group A(primary)

    Group B(Added under primary with users XXX,YYY,ZZZ)

    Trying with the below code 

    $users=Import-Csv somepath\users.csv
    $groups=Import-Csv somepath\groups.csv
    $primarygroup=Get-ADGroupMember -Identity $groups -Recursive | select -ExpandProperty Name
    $secondarygroup=Get-ADGroupMember -Identity "GroupB" -Recursive | select -ExpandProperty Name
    
    Foreach ($Users In Get-ADGroupMember -Identity "GroupB")
    {
        # If they are a 'MemberOf' GroupA
        If ((Get-ADUser $Users.SamAccountName -Properties MemberOf).MemberOf -Contains $primarygroup)
        {
            # Remove that user from GroupA
           # Remove-ADGroupMember -Identity "Group A" -Members $User.SamAccountName
        }
    }

    Tuesday, September 17, 2019 1:01 PM

All replies

  • So, in your example, user YYY will be removed from group A and added to group B. But nothing will be done with users XXX and ZZZ since they are already members of group B? Or do you intend that group A will always be empty after running the script?

    Edit: Also, "primary" group has a meaning in Active Directory, which I suspect is not your meaning. Perhaps you mean parent and child groups, where the child group is a member of (nesting within) the parent.

    Of course, there are many ways to reveal membership in groups that will show both direct members, plus members due to group nesting (recursive membership). So perhaps, there is little need to remove the hierarchy.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Tuesday, September 17, 2019 1:22 PM
  • Once on comparing the Group A and Group B , if same members are in the both the group ,

    I need to remove the members XXX and ZZZ in the Primary group and only the Group B should be the part of Group A

    Tuesday, September 17, 2019 1:31 PM
  • Sorry, I was editing my reply when you responded. Please review my comments on nomenclature to avoid confusion. For example, which is the parent group and which the nested child group?

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, September 17, 2019 1:39 PM
  • Thanks Richard, 

    Group A will be the parent group and Group B will be the child group.

    I will take the Parent Group in a CSV file and the Child group will be one Group (Default group-Group B) . 

    Need to compare the Parent Group with Group B for users availability and move the missing user to Group B 

    No user should be there in the Parent group and everyone should be in the child group(Group B). 

    Hope it helps 

    Tuesday, September 17, 2019 1:46 PM
  • Richard

    Group A will be the parent group and Group B will be the child group.

    I will take the Parent Group in a CSV file and the Child group will be one Group (Default group-Group B) . 

    Need to compare the Parent Group with Group B for users availability and move the missing user to Group B 

    No user should be there in the Parent group and everyone should be in the child group(Group B). 

    Thursday, September 19, 2019 1:37 PM