Please let us know how useful you find this tip by rating it below. Do you have a useful Windows tip, timesaver
or workaround to share? Submit it to our tip contest and you could win a prize!
This sample script shows you how to create a user and with a home directory, and also sets the necessary permissions on that directory.
The script creates a user and adds additional properties such as telephone number and title, sets the password and enables the user's account. It also creates the user's home folder, sets administrators to have "full" control and the user to have "change" control on the folder. If you view the command line switches for CACLS.EXE and make the appropriate changes in the script, you can easily modify the script to the set permissions to fit the requirements of any environment.
Copy and paste the following script into Notepad (make sure you have Word Wrap disabled) and then save it with a .vbs extension:.
================================================ Option Explicit Const WAIT_ON_RETURN = True Const HIDE_WINDOW = 0 Const USER_ROOT_UNC = "\\dc1\users" 'Set Home Folder Location Here Dim WshShell, WshNetwork, objFS, objServer, objShare Set WshShell = Wscript.CreateObject("Wscript.Shell") Set WshNetwork = WScript.CreateObject("WScript.Network") Set objFS = CreateObject("Scripting.FileSystemObject") Set ou = GetObject("LDAP://OU=Users,OU=Billing,OU=Network,DC=my,DC=domain,DC=com") 'Create the User Set usr = ou.Create("user", "CN=James Smith") usr.Put "samAccountName", "jsmith" usr.Put "sn", "Smith" usr.Put "givenName", "James" usr.Put "userPrincipalName", "firstname.lastname@example.org" usr.Put "telephoneNumber", "(555) 555 0111" usr.Put "title", "Network Billing Dept" usr.SetInfo
Now that you created the user, reset the user's password and enable the account. usr.SetPassword "secret***!" usr.AccountDisabled = False usr.SetInfo
Now create the user's home folder and set permissions. strUser = usr.samAccountName Call objFS.CreateFolder(USER_ROOT_UNC & "\" & strUser) Call WshShell.Run("cacls " & USER_ROOT_UNC & "\" & strUser & _ " /e /g Administrators:F", HIDE_WINDOW, WAIT_ON_RETURN) Call WshShell.Run("cacls " & USER_ROOT_UNC & "\" & strUser & _ " /e /g " & strUser & ":C", HIDE_WINDOW, WAIT_ON_RETURN) ====================================================
NOTE: Make sure you have the latest scripting engines on the workstation you run this script from. You can download the latest scripting engines here: Microsoft Scripting Home Page
In addition, when working with the Active Directory Services Interface (ADSI), you must have the same applicable rights, as you would use the built-in administrative tools.
ABOUT THE AUTHOR: Hans Schefske is an Active Directory and SMS Architect for Management Decisions Inc., a telecommunications company based in Atlanta. He was awarded a Microsoft Most Valuable Professional (MVP) Award for SMS in 2003. Hans can be contacted at: email@example.com.