
PowerShell Basics: Copy AD Group Members to Another Group
Have you ever needed to create a new Active Directory group but needed to mirror the users from another AD group? And no, you shouldn’t be nesting groups, especially if you work in Entra ID (Azure AAD). For groups with many members, this can be a difficult or very manual, you would think an AD copy and paste group would exist!
Have no fear, PowerShell will do this very nicely for you. Below is a script that I created to copy all the members from Group1 to Group2. Adjust the script as needed, and run as admin.
# Define the groups
$sourceGroup = "Group1"
$destinationGroup = "Group2"
# Import Active Directory module
Import-Module ActiveDirectory
# Get the members of the source group
$sourceGroupMembers = Get-ADGroupMember -Identity $sourceGroup
# Add each member to the destination group
foreach ($member in $sourceGroupMembers) {
Add-ADGroupMember -Identity $destinationGroup -Members $member
}
Write-Output "All users from $sourceGroup have been copied to $destinationGroup."
This will print out a nice confirmation in the end for you.
PS C:\Windows\system32> # Define the groups
$sourceGroup = "Group1"
$destinationGroup = "Group2"
# Import Active Directory module
Import-Module ActiveDirectory
# Get the members of the source group
$sourceGroupMembers = Get-ADGroupMember -Identity $sourceGroup
# Add each member to the destination group
foreach ($member in $sourceGroupMembers) {
Add-ADGroupMember -Identity $destinationGroup -Members $member
}
Write-Output "All users from $sourceGroup have been copied to $destinationGroup."
All users from Group1 have been copied to Group2.
That should save you some keyboard and clicking time.
Hope you find this helpful!
Categories
Recent Posts
- PowerShell: How to Add an Alias to Every Users Mailbox and Groups in Microsoft 365
- Slack: Disable Entra ID User using a slash command.
- Slack: Retrieve Entra ID (MS365) User Information with a slash command.
- Jira Cloud: Disabling Entra ID User Accounts via Automation and Microsoft Runbook
- Jira Cloud: Restart an Azure VM using JSM Assets and Automation