public static void AddMemberToGroup(string userName, string groupName)
        {
            try
            {
                PrincipalContext AD    = new PrincipalContext(ContextType.Domain);
                GroupPrincipal   group = GroupPrincipal.FindByIdentity(AD, IdentityType.SamAccountName, groupName);
                Console.WriteLine("Group: " + group.ToString());
                group.Members.Add(AD, IdentityType.SamAccountName, userName);
                group.Save();
                Console.WriteLine("User \"" + userName + "\" added to group \"" + groupName);
            }

            catch (Exception e)
            {
                Console.WriteLine("An error occurred. {0}", e.Message);
            }
        }
        /// <summary>
        /// getUsersInGroup will return all users in the group passed in as a parameter
        /// the names returned are the SAM Account Name of the users.
        /// The function will recursively search all nested groups.
        /// </summary>
        /// <param name="strGroupDN">DN of the group, which the users should be retrieved from</param>
        /// <returns>ArrayList containing the SAM Account Names of all users in this group and any nested groups</returns>
        public ArrayList getUsersInGroup(string strGroupDN)
        {
            ArrayList groupMembers = new ArrayList();
            //string strDomains = "playasur,mtvn,mtvne,viacom_corp,mtvnasia,paramount,ad,corp";
            //string[] strDomainArr = strDomains.Split(',');
            const string Domain = "viacom_corp.ad.viacom.com";

            ////for (int i = 0; i < (strDomainArr.Length); i++){
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Domain);

            GroupPrincipal    qbeGroup = new GroupPrincipal(ctx, "LTGroup");
            PrincipalSearcher srch     = new PrincipalSearcher(qbeGroup);

            //    // find all matches
            foreach (var found in srch.FindAll())
            {
                GroupPrincipal foundGroup = found as GroupPrincipal;

                if (foundGroup != null)
                {
                    //if (foundGroup.ToString().ToLower().Contains("group"))
                    //Group Name
                    groupMembers.Add(foundGroup.ToString());
                    foreach (Principal p in foundGroup.GetMembers(true))
                    {
                        //Member
                        groupMembers.Add(p.Name);
                    }

                    // do whatever you need to do, e.g. put name into a list of strings or something
                }
            }
            ////}

            //SearchResult result;
            //DirectorySearcher search = new DirectorySearcher("LDAP://mtvn.ad.viacom.com");
            //search.Filter = String.Format("(cn={0})", "IT Group");
            //search.PropertiesToLoad.Add("member");
            //result = search.FindOne();

            //searchedGroups = new Hashtable();
            //searchedGroups.Add(strGroupDN, strGroupDN);
            //strGroupDN = "IT Group";
            //// find all users in this group
            //DirectorySearcher ds = new DirectorySearcher("LDAP://mtvn.ad.viacom.com");
            //ds.Filter = String.Format("(&(memberOf={0})(objectClass=person))", "IT Group");

            //ds.PropertiesToLoad.Add("distinguishedName");
            //ds.PropertiesToLoad.Add("givenname");
            //ds.PropertiesToLoad.Add("samaccountname");
            //ds.PropertiesToLoad.Add("sn");

            //foreach (SearchResult sr in ds.FindAll())
            //{
            //    groupMembers.Add(sr.Properties["samaccountname"][0].ToString());
            //}

            //// get nested groups
            //ArrayList al = getNestedGroups(strGroupDN);
            //foreach (object g in al)
            //{
            //    if (!searchedGroups.ContainsKey(g)) // only if we haven't searched this group before - avoid endless loops
            //    {
            //        // get members in nested group
            //        ArrayList ml = getUsersInGroup(g as string);
            //        // add them to result list
            //        foreach (object s in ml)
            //        {
            //            groupMembers.Add(s as string);
            //        }
            //    }
            //}


            ArrayList al = getNestedGroups(strGroupDN);

            return(groupMembers);
        }