If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement
(S.DS.AM) namespace. Read all about it here:
- Managing Directory Security Principals in the .NET Framework 3.5
- MSDN docs on System.DirectoryServices.AccountManagement
Basically, you can define a domain context and easily find users and/or groups in AD:
// set up context to your local machine only
using (PrincipalContext ctx = new PrincipalContext(ContextType.Machine))
{
// find your user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if(user != null)
{
// if user is found - remove it
user.Delete();
}
}
The new S.DS.AM makes it really easy to play around with users and groups in AD!