vbscript to create home directory if it does not exist

Posted on 2009-04-23
Last Modified: 2013-11-10
I'm running a login script that will map some network drives for the users when they log in.  I'd like for the script to do a check to see if the users home directory, \\SERVER\Users$\<USER> exists.  If the folder exist great, it can continue on.  If it does not exist then I would like the script to create the folder and assign the user permissions to the folder.  Any ideas on how I can accomplish this goal?



Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path



Set WSHShell = CreateObject("WScript.Shell")

Set WSHNetwork = CreateObject("WScript.Network")

'Automatically grab the user's domain name

DomainString = Wshnetwork.UserDomain

'Find the Windows Directory

WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")


'Grab the user name

UserString = WSHNetwork.UserName

'Bind to the user object to get user name and check for group memberships later

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)


'Grab the computer name for use in add-on code later

strComputer = WSHNetwork.ComputerName




'Disconnect ALL mapped drives

Set clDrives = WshNetwork.EnumNetworkDrives

For i = 0 to clDrives.Count -1 Step 2

    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True



'Give the PC time to do the disconnect, wait 500 milliseconds

wscript.sleep 500


'Map drives needed by all

'Note the first command uses the user name as a variable to map to a user share.




WSHNetwork.MapNetworkDrive "M:", "\\SERVER\users$\" & UserString ,True

WSHNetwork.MapNetworkDrive "I:", "\\SERVER\Information",True

Open in new window

Question by:lakeview108
    LVL 38

    Accepted Solution

    The script below, inserted at line 21, should do it.  This assumes that the user has sufficient permissions to create the folder and change permissions on it.

    strHomeDir = "\\SERVER\Users$\" & UserString
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FolderExists(strHomeDir) Then
        WSHShell.Exec("%comspec% /c echo Y|cacls " & Chr(34) & strHomeDir & Chr(34) & _
            " /C /G " & UserString & ":F SYSTEM:F")
    End If

    Open in new window


    Author Comment

    I forgot to mention that the users don't have permissions to create the folders at that level.  Is it possible to use the admin account to create the folder?
    LVL 38

    Assisted Solution

    No, or at least not easily.  You don't want to include a password in a script, because users could view it.

    Users' home folders should get automatically created when their accounts are set up.  If some are missing then it might be better to use an AD query to mass-create them for all of your users at once.

    Author Comment

    Okay thanks for all of the help.  I'll investigate the AD query and see what I can come up with.  Shift-3, you've been a great help.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    A short article about a problem I had getting the GPS LocationListener working.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now