Directory Services

Creating Local Users

The following steps describe how to create a local user account on a computer:

  1. You must use the WinNT provider to create user or group accounts on a local machine. The LDAP provider cannot be used because local machines do not have an LDAP head to process LDAP commands.
  2. Call the Create method of the computer object, specifying the user class and the account name.
  3. Call the SetPassword method of the user object. Be sure that the password satisfies the computer's password policy requirements, such as minimum password length.
  4. Set other properties of the user object.
  5. Call the SetInfo method of the user object to save the new user object to the underlying directory storage.

Be aware that with the WinNT provider, you must set the password before calling SetInfo. Otherwise, the account creation will fail if the account does not satisfy local password policy requirements. This differs from the creation of domain user accounts using the LDAP provider, in which you must call SetInfo before you can set the password, and the account is disabled if it does not meet password policy requirements. For more information about creating domain user accounts with the LDAP provider, see Creating a User.

The following code example creates a new user and a sets important properties for that user.

' Set up property values for the new user
Dim sUsername ' String that will contain the username 
Dim sFullName ' String that will contain the user's full name
Dim sDescription ' String that will contain a description of the user
Dim sPassword ' String that will contain the user's password
Dim sComputerName ' String that contains the name of the computer

On Error Resume Next

' Insert code that will safely retrieve the username, full name,
' description, password, and computer name.

Set myComputer = GetObject("WinNT://" & sComputerName)
If Err.Number<>0 Then
	MsgBox ("An error has occurred.")
	Set myComputer = Nothing
	Exit Sub
End If

' Create the new user account
Set newUser = myComputer.Create("user", sUsername)
If Err.Number<>0 Then
	MsgBox ("An error has occurred.")
	Set myComputer = Nothing
	Set newUser = Nothing
	Exit Sub
End If

' Set properties in the new user account
newUser.SetPassword sPassword 
newUser.FullName = sFullName
newUser.Description = sDescription

newUser.SetInfo

Set newUser = Nothing
Set myComputer = Nothing