vbscript to create home directory if it does not exist

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

Who is Participating?
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

lakeview108Author Commented:
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?
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.
lakeview108Author Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.