We help IT Professionals succeed at work.

Script to map home drive and department folder

kolathaya123 asked

I have windows 2008 domain environment and a file server. I have requirement of mapping HOme drive and department drives using logon script, How I can automate this.

I have folder structure as below:




I have security group like accounts, HR, purchase and users in it.

When a user "John" from HR department logs in, he must me mapped with
H:\  = \\fileserver\home\john
Z:\  = \\fileserver\HR

When a user "Mary" from accounts department logs in, she must me mapped with
H:\  = \\fileserver\home\Mary
Z:\  = \\fileserver\accounts

How I can do this using any script.
Watch Question

Technology and Business Process Advisor
Most Valuable Expert 2013
Ideally you should be using Group Policy Preferences to map network drives.  This Directory Services team blog illustrates how to use Group Policy preferences for this purpose:

If you must use a script, Microsoft has a blog on doing so with VBS - the script from that blog is excepted below:
On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")

strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)

For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN

    Select Case strGroupName
        Case "Finance Users"
            objNetwork.MapNetworkDrive "X:", "\\atl-fs-01\finance"
        Case "Human Resource Users" 
            objNetwork.MapNetworkDrive "X:", "\\atl-fs-01\hr"

        Case "Manufacturing Users"
            objNetwork.MapNetworkDrive "X:", "\\atl-fs-01\manufacturing"

        Case "Shipping and Receiving Users"
            objNetwork.MapNetworkDrive "X:", "\\atl-fs-01\shipping"
    End Select

Open in new window

Or you could use a batch script using the net user /domain command and additional parameters as the excerpted script below from this blog points out:

net user /domain %username% | find "MYOB Users"
if not errorlevel = 1 (
net use m: \\JCC-SBS\MYOB

Open in new window