Microsoft KB Archive/306271

= How to add a user to the local system by using Directory Services in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 306271

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q306271





For a Microsoft Visual C# .NET version of this article, see 306273.

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Sample
 * Code Explanation
 * Troubleshooting

REFERENCES



SUMMARY
This article describes how to use the DirectoryServices namespace to add a user to the local system and a group.

back to the top

Requirements

 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005

back to the top

Create the Sample
 Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and create a new Visual Basic Console Application project. In Solution Explorer, right-click References, and then click Add Reference. Add a reference to the System.DirectoryServices.dll assembly.  Replace the code in Module1.vb with the following code: Imports System.DirectoryServices Module Module1

Sub Main Try Dim AD As DirectoryEntry = _ New DirectoryEntry(&quot;WinNT://&quot; + Environment.MachineName + &quot;,computer&quot;) Dim NewUser As DirectoryEntry = AD.Children.Add(&quot;TestUser1&quot;, &quot;user&quot;) NewUser.Invoke(&quot;SetPassword&quot;, New Object {&quot;#12345Abc&quot;}) NewUser.Invoke(&quot;Put&quot;, New Object {&quot;Description&quot;, &quot;Test User from .NET&quot;}) NewUser.CommitChanges Dim grp As DirectoryEntry

grp = AD.Children.Find(&quot;Guests&quot;, &quot;group&quot;) If grp.Name <> &quot;&quot; Then grp.Invoke(&quot;Add&quot;, New Object {NewUser.Path.ToString}) End If   Console.WriteLine(&quot;Account Created Successfully&quot;) Console.ReadLine

Catch ex As Exception Console.WriteLine(ex.Message) Console.ReadLine End Try End Sub

End Module  Compile and run the project. Follow these steps on a Windows 2000-based or Windows Server 2003-based computer to verify that the account was created and added to the Guest group:

<ol style="list-style-type: lower-alpha;"> From the Start menu, point to Programs, point to Administrative Tools, and then click Computer Management.</li> Click to expand the Local Users and Groups node. The new account should appear under the Users node, as well as under the node for the Guest group.</li></ol>

Follow these steps on a Windows XP-based computer to verify that the account was created and added to the Guest group: <ol style="list-style-type: lower-alpha;"> From the Start menu, click Control Panel.</li> Double-click User Accounts. The new user account should appear in the User Accounts dialog box.</li></ol> </li> Importantly, remove the newly created user account from the system after you finish testing.</li></ol>

back to the top

Create a New Directory Entry
When you create the directory entry in this sample, it is assumed that the system is running Microsoft Windows NT, Windows 2000, or Windows XP. Note that the string that is passed to the DirectoryEntry constructor begins with &quot;WinNT://&quot;. You can also run Directory Services on other third-party operating systems. Dim AD As DirectoryEntry = _ New DirectoryEntry(&quot;WinNT://&quot; + Environment.MachineName + &quot;,computer&quot;)

Add the Directory Entry to the Directory Tree
The following code adds a DirectoryEntry of type user with the value of TestUser1 to the Active Directory tree. Dim NewUser As DirectoryEntry = AD.Children.Add(&quot;TestUser1&quot;, &quot;user&quot;)

Set the Password and Description for the New User Account
The following code calls the Invoke method to invoke the SetPassword and Put methods of the DirectoryEntry object. This sets the password and assigns a description to the user account. This code also calls the CommitChanges method to save the changes. NewUser.Invoke(&quot;SetPassword&quot;, New Object {&quot;#12345Abc&quot;}) NewUser.Invoke(&quot;Put&quot;, New Object {&quot;Description&quot;, &quot;Test User from .NET&quot;}) NewUser.CommitChanges

Add the Account to a Group
The first step to add the account to a group is to define a variable of type DirectoryEntry. Then you call the Find method of the Children member of the ActiveDirectory class to populate the variable. In this case, the Guest group is the target of the search. This code tests the value that the Find method returns to determine if the group has been found. If the group is found, the new user account is added to the group. Dim grp As DirectoryEntry grp = AD.Children.Find(&quot;Guests&quot;, &quot;group&quot;) If grp.Name <> &quot;&quot; Then grp.Invoke(&quot;Add&quot;, New Object {NewUser.Path.ToString}) End If back to the top

Troubleshooting
The code in this article fails if you try to run the code without the sufficient privileges to create a user account. For the code to complete successfully, the currently logged on user must be a member of the Administrators group or have specific permissions that allow the user to create user accounts.

back to the top

<div class="references_section">