If User is Member of Group A, Remove Group A, then add to Group B

Jon Hatfield 46 Reputation points
2021-01-14T23:26:26.657+00:00

Title says it all. I'm trying to create a PowerShell script that will do the following:

Search for all members of Group A

Remove from Group A

Add to Group B

Any tips are appreciated!

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,984 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,404 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 98,621 Reputation points MVP
    2021-01-14T23:58:26.547+00:00

    The script should look like this (not tested!)

    Import-Module ActiveDirectory  
    Get-ADGroupMember -Identity "GroupA" |  
            ForEach-Object {Add-AzADGroupMember -Identity "GroupB" -Members $_  
                            Remove-ADGroupMember "GroupA" $_ -Confirm:$false  
                           }  
    

    https://learn.microsoft.com/en-us/powershell/module/addsadministration/get-adgroupmember?view=win10-ps
    https://learn.microsoft.com/en-us/powershell/module/addsadministration/add-adgroupmember?view=win10-ps
    https://learn.microsoft.com/en-us/powershell/module/addsadministration/remove-adgroupmember?view=win10-ps

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jon Hatfield 46 Reputation points
    2021-01-15T19:35:18.317+00:00

    That worked perfectly. Thank you!

    0 comments No comments