Click here to Skip to main content
12,559,868 members (48,156 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked

Copy AD Members From One Group to Another using C#

, 20 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Copy members between Active Directory groups


Recently my company merged two locations into one, and we needed to also merge some Active Directory groups. I wrote a console command in C# to move users from old groups to another group using System.Directory.AccountManagement namespace classes.

Using the Code

There are only two classes, the Program class, and the CopyADGroup class. The CopyADGroup has the CopyGroup method, which takes three parameters: the group whose users are being copied, the group being copied to, and the domain name. These parameters will be supplied at the command line in that order.

Command-line example:

CopyADGroup oldgroup, newgroup,
using System;
namespace CopyADGroup
    class Program
	   static void Main(string[] args)
		  if(args.Length != 3)
			 Console.WriteLine("Must enter three parameters for this command. 
			 The group whose members you want to copy, the group to which you want 
			 to copy those members, and domain name for the groups.");
			 CopyGroup Copying = new CopyGroup();
			 //args[0] is the group to retrieve members from, args[1] 
			 //is the group to which those members will be added, 
			 //args[2] is the domain name.
			 Copying.CopyFromTo( args[0], args[1], args[2]);
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
namespace CopyADGroup
    //This class retrieves Active Directory user accconts from the first group, 
    //and adds those accounts to the second
    //group passed into the CopyFrom method.

    public class CopyADGroup
	   public void CopyFromTo
	   (string CopiedGroup, string CopiedToGroup, string DomainName)
			 //The domain name was included to avoid an "Unknown Error" 
			 //in the AccountManagement reference. This is supposed to have
			 //been fixed in .NET 4.5.
			 using (PrincipalContext PC = 
			 	new PrincipalContext(ContextType.Domain, DomainName))
				using (GroupPrincipal FirstGroup = GroupPrincipal.FindByIdentity
				(PC, IdentityType.SamAccountName, CopiedGroup))
				    //Retrieve the group members to be copied.
				    var FirstGroupMembers = FirstGroup.GetMembers(true);
				    //Retrieve the group to which the users will be added.

				    using (GroupPrincipal SecondGroup = 
				    	(PC, IdentityType.SamAccountName, CopiedToGroup))
					   foreach (Principal User in FirstGroupMembers)
						  string UserName;
						  //Check if the user is already a member 
						  //of the second group to avoid an error.
						  if (!User.IsMemberOf(SecondGroup))
							 UserName = User.SamAccountName;
							 (PC, IdentityType.SamAccountName, UserName);
		  catch (Exception Ex)
			 Console.WriteLine("Error: " + 
			 	Ex.Message.ToString() + " " + Ex.StackTrace);

Points of Interest

Note that if the old group has a group as a member, it will copy the users in that group to the second group, instead of the member group itself.

The PrincipalContext object includes the domain name to avoid an error I was getting when I only included ContextType.Domain as a parameter -

System.DirectoryServices.AccountManagement.PrincipalOperationException] = 
	{"Unknown error (0x80005000)"}


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Timothy Gutzke
Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionYour text does not matches the example Pin
JV999921-Jan-14 3:14
professionalJV999921-Jan-14 3:14 
AnswerRe: Your text does not matches the example Pin
Timothy Gutzke21-Jan-14 6:51
professionalTimothy Gutzke21-Jan-14 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161026.1 | Last Updated 20 Jan 2014
Article Copyright 2014 by Timothy Gutzke
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid