Click here to Skip to main content
13,666,696 members
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 2 Aug 2006
Licenced CPOL

UserManager: a class to manipulate local Windows users and groups

, 2 Aug 2006
Rate this:
Please Sign up or sign in to vote.
UserManager was built to simplify local users and groups manipulation


UserManager class contains a set of methods to create/modify local Windows users and groups using System.DirectoryServices namespace. ADSI classes in Directory Services namespace enable programmers to access ADSI objects using System.DirectoryServices namespace.


By using UserManager class, a developer can use the following methods:

  • AddUser
  • RemoveUser
  • SetUserPassword
  • EnableUser
  • DisableUser
  • AddOptionFlagToUser
  • RemoveOptionFlagToUser
  • UserProperties
  • ListUsersInServer
  • AddGroup
  • GroupProperties
  • AddUserToGroup
  • ListUsersInGroup
  • ListGroupsInServer

The following code shows how to use UserManager class to create a new local user.

string loginName = "TestUser";
string loginPwd = "PwdTestUser";
string loginDescription = "Descr TestUser";
string defaultGroupName = "Users";

UserManager um = new UserManager();
if (!um.AddUser(loginName, loginPwd, loginDescription, defaultGroupName))
    MessageBox.Show(um.ErrorMessage, "Warning", MessageBoxButtons.OK);
    MessageBox.Show("User " + loginName + " created", "Warning",

um = null;

The following is the AddUser code:

public bool AddUser(string LoginName, string LoginPassword,
            string LoginDescription, string defaultGroup)
    bool created = false;

        using (DirectoryEntry AD = new
                   DirectoryEntry("WinNT://" + Environment.MachineName + ",
            bool found = false;

            // check if "LoginName" already exists
                found = AD.Children.Find(LoginName, "user") != null;
                found = false;

            if (!found)
                // create a new directory entry
                using (DirectoryEntry NewUser =
                           AD.Children.Add(LoginName, "user"))
                    // set password
                    NewUser.Invoke("SetPassword", new object[] 
                            { LoginPassword });
                    // set description
                    NewUser.Invoke("Put", new object[] {"Description", 

                    // commit changes

                    // set default options
                    //     UF_NORMAL_ACCOUNT
                    //     UF_PASSWD_CANT_CHANGE
                    //     UF_DONT_EXPIRE_PASSWD
                    // you can change this

                    created = true;

                    // if defaultGroup is not null
                    // try to add new user to it
                    if ((defaultGroup != null) && 
                    (defaultGroup.Trim().Length > 0))
                        DirectoryEntry grp = null;

                            // find defaultGroup entry
                            using (grp = AD.Children.Find(defaultGroup, 
                                if (grp != null)
                                    // add new user to group
                                    grp.Invoke("Add", new object[] 
                        { NewUser.Path.ToString() });
                        catch (Exception ex)
                            aErrMsg = ex.Message;

                aErrMsg = "User already exists!";

    catch (Exception ex)
        aErrMsg = ex.Message;

    return created;

The following image shows the demo program, included in the zip file, which uses the UserManager class to list Users and Groups in your PC.


The download file at the top of this article contains UserManager source code (.NET 2.0 and .NET 1.1 versions) and a demo project (.NET 2.0).

I hope you enjoy this article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Massimo Beatini
Web Developer
Italy Italy
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionGet List of groups to which a user is a "Member Of" Pin
beckman1616-Apr-09 20:41
memberbeckman1616-Apr-09 20:41 
QuestionAdding domain user in Administrator Group Pin
Mushq6-Aug-08 23:05
memberMushq6-Aug-08 23:05 
GeneralFailed to create new users Pin
andre_monteiro14-Jan-08 7:48
memberandre_monteiro14-Jan-08 7:48 
GeneralRe: Failed to create new users Pin
el_guerrero_nocturno7-May-09 23:12
memberel_guerrero_nocturno7-May-09 23:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 2 Aug 2006
Article Copyright 2006 by Massimo Beatini
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid