Directory Services

Creating Users

The following code example shows how to create a user in an organizational unit.

DirectoryEntry ent = new DirectoryEntry();
DirectoryEntry ou = ent.Children.Find("OU=Consulting");

// Use the Add method to add a user in an organizationalUnit.
DirectoryEntry usr = ou.Children.Add("CN=New User","user");
// Set the samAccountName, then commit changes to the directory.
usr.Properties["samAccountName"].Value = "newuser"; 

The samAccountName property is set in this example. The samAccountName creates a unique samAccountName, such as $CP2000-O16B1V0UKHK7. This property is used to support clients and servers from Windows NT 4.0 and earlier, Windows 95, Windows 98, and LAN Manager. In Windows Server 2003, the samAccountName property is optional.