Map Network Drives

By Christopher Stone
Published Dec 1, 2010
CC By-SA Licensed
Now that XP is officially dead and gone there's no reason to be using this script anymore. Group Policy Preferences is a better way to map drives on Windows Vista/2008 and newer (GPP was horribly buggy in XP, hence the need for the script at that time). I no longer use this script and do not recommend anyone use it unless they have unsupported XP clients. Mapping network drive as login can be a bit of a chore. There might be existing mapped drives, sometimes to the wrong location, or other considerations. To that end I wrote a short VBScript to help the process along. Group Policy can assign login scripts to users, so all that's needed is to edit this script to fit your environment, pop it in the \\domain.name\netlogon folder, and point to it from a GPO.

The script source:
' Author: Chris Stone
' Date: 29 MAY 2009  Version: 1.3
' Purpose: Map network drives

On Error Resume Next
Set objNet = CreateObject("WScript.Network")

Public Sub CheckAndMapNetDrive(Letter, Path, Persist)
  'Check if drive letter is already used
  Set colNetDrives = objNet.EnumNetworkDrives
  For i = 0 To colNetDrives.Count - 1 Step 2
    If colNetDrives.Item(i) = Letter Then
      'Drive Letter Exists, Test if it's the same Path
      If colNetDrives.Item(i+1) = Path Then
        'It's the same, no new mapping necessary.
        Exit Sub
      Else
        'It's different, remove old.
        objNet.RemoveNetworkDrive colNetDrives.Item(i)
      End If
    End If
  Next
  'Drive does not exist now, never did or removed.
  objNet.MapNetworkDrive Letter, Path, Persist
End Sub  

' Edit the following to fit your environment!
' CheckAndMapNetDrive [Drive_Letter], [Net_Path], [Bool_Persist]

CheckAndMapNetDrive "R:", "\\server1\share1", True
CheckAndMapNetDrive "S:", "\\server1\homes\" & objNet.UserName, True
CheckAndMapNetDrive "T:", "http://webdav.example.com", False
The example includes a home drive, S: mapped to \\server1\homes\username. There are other objects you can pull into the path if you need to map a drive based on various requirements. You are not limited CIFS shares with this script either, as the example shows, WebDav servers can be mapped as well as FTP servers. This can be especially useful when combined with SharePoint sites or similar document management solutions.

As stated above, edit this script to map the drives you need, then save a copy in the \\domain.com\netlogon share (it can be placed elsewhere, but this is traditionally the standard location for login scripts). Remember to properly document how and why you are mapping network drives. Test the script to be sure it is working as expected.

After the file is edited, saved, and tested; open Group Policy Management. Create or Edit a GPO for the Domain or OU as is appropriate for your environment. Find Logon under User Configuration > Policies > Windows Settings > Scripts. Open Logon's properties, click the Add button and enter the path where you placed the script; no script parameters are necessary unless you added parameter handling ability to the script. OK your way back through the screens and close it out.

Your clients will have to refresh their policy before this will take effect. That usually happens before login and every ~30 minutes (but these are the default settings); run gpupdate on a client to force the update.

This script works on Windows 2000 and newer Windows OSes, both 32-bit and 64-bit.