Thursday, March 3, 2016

Copying users from one AD group to another

How to copy the group membership of one AD group to another

$FromG = "MySourceGroup"
$ToG = "MyDestinationGroup"

Add-ADGroupMember $ToG -member ((Get-ADGroupMember $FromG).DistinguishedName)

Well, that's all fine but the script will choke if the source group has any of the following characteristics:

  • More than 1,500 objects inside
  • Has any foreign security principles inside (members of trusted domains)
  • Has any nested groups inside
Here is a more robust version:


$FromG = "MySourceGroup"
$ToG = "MyDestinationGroup"

Get-ADGroup $FromG -Properties Member |Select-Object -ExpandProperty Member|ForEach-Object `
{
Add-ADGroupMember $ToG -Member $_
}



Cheers!

No comments:

Post a Comment