Click here to Skip to main content
12,816,219 members (32,640 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 19 Feb 2003

How to add a new user using DirectoryServices?

, 19 Feb 2003
Rate this:
Please Sign up or sign in to vote.
An article describing the use of DirectoryServices namespace classes in .NET on how to add a new user in a machine or a domain.


In our previous article, How to get full name of logged in user, we showed how you can get full name of a user in a given domain or machine. You can extend that idea to obtain any information you want for a given user. This artcile will describe how you can add a new user account into your domain or machine using .Net DirectoryServices classes. We will be using WinNT provider for illustrations in this article. But you can extend the examples to use LDAP or AD providers.


Here are the key steps that you will need to perform to create new account.

  • Create a new DirectoryEntry object and specify the machine name as the path.
  • User accounts are created as nodes corrresponding to User schema class in Active Directory. Therefore we will add a new DirectoryEntry object in Children collection of the machine. The key thing to rememeber will be that when you add new entry, make sure that the schema class name is User.
  • When you add a new node in Children collection, it will return you the newly created object. At this stage the information has not been added to your machine or active directory tree.
  • Now you can set all the values that you need to set for a given account. Following is the list of property names that you can set for the account.
    • UserFlags
    • MaxStorage
    • PasswordAge
    • PasswordExpired
    • LoginHours
    • FullName
    • Description
    • BadPasswordAttempts
    • LastLogin
    • HomeDirectory
    • LoginScript
    • Profile
    • HomeDirDrive
    • Parameters
    • PrimaryGroupID
    • Name
    • MinPasswordLength
    • MaxPasswordAge
    • MinPasswordAge
    • PasswordHistoryLength
    • AutoUnlockInterval
    • LockoutObservationInterval
    • MaxBadPasswordsAllowed
    • RasPermissions
    • objectSid

    For more information on these properties please read Active Directory Services Interface(ADSI) section in Microsoft Platform SDK.

  • You must have noticed from the above list that there is no property to set or get user password value. Operating system does not give access to clear text password value. So we can't expect and property or method to get it. In ADSI, IAdsUser interface provides SetPassword method to set a user's password. This is where Invoke method of DirectoryEntry class comes handy. So we call Invoke to set the password value. The Invoke method can be used to call native methods on underlying active directory objects. There is one important thing to remeber when you set a user's password value. If you are using LDAP provider, then the user account should already have been created in the system by calling CommitChanges or SetInfo method. But WinNT provider does not have this restriction. You can set password value without commiting the changes first.
  • The last step would be to actually create the account in the machine or domain. This is done by calling CommitChanges method on newly added DirectoryEntry object.

The following code demonstrates all the steps that we described above.

private void AddUser(string strDoamin, string strLogin, string strPwd)
    DirectoryEntry obDirEntry = null;
        obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
        DirectoryEntries entries = obDirEntry.Children;
        DirectoryEntry obUser = entries.Add(strLogin, "User");
        object obRet = obUser.Invoke("SetPassword", strPwd);
    catch (Exception ex)

Please feel free to send your suggestions directly to us at


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
To learn more about us, Please visit us at

You may also be interested in...


Comments and Discussions

GeneralCreating Group Pin
sreejith ss nair30-Jan-08 0:38
membersreejith ss nair30-Jan-08 0:38 
Generalactivedirectory user profile Pin
Jayesh Talsaniya21-May-07 2:17
memberJayesh Talsaniya21-May-07 2:17 
QuestionHow to delete user? Pin
Gx Lam19-Oct-06 7:12
memberGx Lam19-Oct-06 7:12 
QuestionHow can I grant Administrative right to the newly created User? Pin
Atif Bashir4-Aug-06 1:26
memberAtif Bashir4-Aug-06 1:26 
GeneralSet Primary Group Pin
SirLoric4-Sep-05 12:48
memberSirLoric4-Sep-05 12:48 
Generalwindows 2003 Pin
Quinton Viljoen15-Jun-05 3:29
memberQuinton Viljoen15-Jun-05 3:29 
General"General access denied error\r\n" Pin
dmh18119-May-05 21:48
memberdmh18119-May-05 21:48 
GeneralRe: "General access denied error\r\n" Pin
Naresg23-Aug-05 10:16
sussNaresg23-Aug-05 10:16 
GeneralRe: "General access denied error\r\n" Pin
nano2k21-Nov-05 22:22
membernano2k21-Nov-05 22:22 
GeneralRe: "General access denied error\r\n" Pin
mohdmo200516-Jul-07 23:45
membermohdmo200516-Jul-07 23:45 
GeneralRe: "General access denied error\r\n" Pin
Member 184632829-Sep-08 3:11
memberMember 184632829-Sep-08 3:11 
AnswerRe: "General access denied error\r\n" Pin
dbbtvlzfpz19-Oct-08 23:27
memberdbbtvlzfpz19-Oct-08 23:27 
GeneralAdd user to AD too slow Pin
sonnv2-Mar-05 22:39
membersonnv2-Mar-05 22:39 
Generaladd user problem Pin
sanaz_14-Aug-04 21:48
membersanaz_14-Aug-04 21:48 
GeneralAuthentication issue Pin
Anonymous5-Mar-04 3:47
sussAnonymous5-Mar-04 3:47 
Questionand in ? Pin
trollibus20-Jan-04 4:37
membertrollibus20-Jan-04 4:37 
AnswerRe: and in ? Pin
anonymous11-Feb-04 10:05
sussanonymous11-Feb-04 10:05 
GeneralClickety Pin
Colin Angus Mackay6-Oct-04 6:00
memberColin Angus Mackay6-Oct-04 6:00 
Generalget current user's password Pin
cronosxfiles9-Dec-03 1:32
membercronosxfiles9-Dec-03 1:32 
GeneralExchange Pin
candan18-Aug-03 15:11
membercandan18-Aug-03 15:11 
GeneralGroup membership Pin 0:18 0:18 
GeneralRe: Group membership Pin
cerda29-Mar-04 23:07
membercerda29-Mar-04 23:07 
QuestionRe: Group membership Pin
Atif Bashir8-Aug-06 0:19
memberAtif Bashir8-Aug-06 0:19 
GeneralSetting a UserFlag to Don't Expire a Password Pin
optimistck18-Mar-03 5:46
sussoptimistck18-Mar-03 5:46 
GeneralRe: Setting a UserFlag to Don't Expire a Password Pin
Optimistck18-Mar-03 6:39
memberOptimistck18-Mar-03 6:39 

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 | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 20 Feb 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid