Deleting Groups
The following code example shows how to delete a group using the DirectoryEntries method called Remove. For this task, find the group to delete, which, in the example, is Consulting, then run the Remove method.
[C#]
//Bind to the current domain. DirectoryEntry dom = new DirectoryEntry(); //Use the Find method to find the Consulting OU DirectoryEntry ou = dom.Children.Find("OU=Consulting"); //To delete a group, bind to the group within the OU DirectoryEntry group = new DirectoryEntry(ou + "CN=groupname"); //To delete a manager, bind to the manager object within the OU DirectoryEntry mgr = new DirectoryEntry(ou + "CN=mgrname"); //To delete a distribution list, bind to the distribution list object //within the OU DirectoryEntry dl = new DirectoryEntry(ou + "CN=dlname"); //Use the remove method to remove each of these objects. ou.Children.Remove(group); ou.Children.Remove(mgr); ou.Children.Remove(dl);
[Visual Basic .NET]
'Bind to the current domain. Dim dom As New DirectoryEntry() 'Use the Find method to find the Consulting OU Dim ou As DirectoryEntry = dom.Children.Find("OU=Consulting") 'To delete a group, bind to the group within the OU Dim group As New DirectoryEntry(ou + "CN=groupname") 'To delete a manager, bind to the manager object within the OU Dim mgr As New DirectoryEntry(ou + "CN=mgrname") 'To delete a distribution list, bind to the distribution list object 'within the OU Dim dl As New DirectoryEntry(ou + "CN=dlname") 'Use the remove method to remove each of these objects. ou.Children.Remove(group) ou.Children.Remove(mgr) ou.Children.Remove(dl)