Tip

One script creates directory, permissions

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", "jsmith@my.domain.com"
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.

Editor's Note: If you would like to receive similar tips on how to manage Windows as well as other expert advice, be sure to subscribe to The Administrator Tip newsletter.


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: hschefske@yahoo.com.

This was first published in February 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.