none
Remove all members in a large AD group RRS feed

  • Question

  • Hi,

    I tried the below to clear all members of a group in 1 go but seems like this takes very long to reflect in AD and not all members get deleted due to different DC being picked I guess.I also tried to target specific DC,but no luck.Pls provide an alternative method to remove all members of AD group in 1 shot.Currently using below:

     var context = new PrincipalContext(ContextType.Domain, "xx.uv.abc.com");
                    var group = GroupPrincipal.FindByIdentity(context, "XYZ123");
                    group.Members.Clear();

    In the above,xx.uv is the DC name and abc.com is domain. XYZ123 is the AD group.


    Thursday, August 30, 2018 1:58 PM

Answers

  • Hi,

    You need to loop through each member and remove. See reference code below - 

    PropertyValueCollection members = group.Properties["member"];
    for (int counter = members.Count - 1; counter >= 0; counter--) 
    {
        members.RemoveAt(counter);
        group.CommitChanges();
        group.Close(); 
    }  

    OR using your code - just add more line at the end as below - 

    group.Save();

    Let us know if it works for you


    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Friday, August 31, 2018 5:05 AM

All replies

  • Hi,

    You need to loop through each member and remove. See reference code below - 

    PropertyValueCollection members = group.Properties["member"];
    for (int counter = members.Count - 1; counter >= 0; counter--) 
    {
        members.RemoveAt(counter);
        group.CommitChanges();
        group.Close(); 
    }  

    OR using your code - just add more line at the end as below - 

    group.Save();

    Let us know if it works for you


    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Friday, August 31, 2018 5:05 AM
  • Hi Vivek

    I guess my code itself worked.The issue was something else with some usernames that couldnt be located in AD and code stopped after exception.

    Friday, August 31, 2018 1:02 PM