Mark
Mark

Reputation:

How to remove windows group account using C#

How to remove windows group account using C#

I'm using this code to remove user account, but I need to remove the whole group.

            DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
            DirectoryEntries users = localDirectory.Children;
            DirectoryEntry user = users.Find(userDn);
            users.Remove(user);

I need to remove the Group with it's users.

Upvotes: 1

Views: 1286

Answers (1)

abmv
abmv

Reputation: 7098

public void Delete(string ouPath, string groupPath)
{
    if (DirectoryEntry.Exists("LDAP://" + groupPath))
    {
        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + ouPath);
            DirectoryEntry group = new DirectoryEntry("LDAP://" + groupPath);
            entry.Children.Remove(group);
            group.CommitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }
    }
    else
    { 
        Console.WriteLine(path + " doesn't exist"); 
    }
}

Howto: (Almost) Everything In Active Directory via C#

Upvotes: 1

Related Questions