Mapping Network Drives Using VBScript

Friday, October 28, 2011

One of the most common login scripts used in a domain environment is one that maps network drives for the end user. There are a few ways that a solution can be implemented such as using group policy (preferred), or for backward compatibility for pre-Windows 2000 systems, using the Profile tab in the user account properties. If the Profile method is used by modifying the user’s attribute “Login Script”, the script itself must be stored in the NETLOGON folder located on the Domain Controllers (DC). Simply copy the file to that location on one of the DCs and the file will be replicated to the remaining DCs in the domain.

Either method can be used. Here is a sample script that can be used to map one or more network drives for a user:

' Map Network Drives, H:, I:, S:, and U:
' -----------------------------------------------------------------'

Option Explicit

Dim objNetwork, strRemotePath1, strRemotePath2, strRemotePath3, strRemotePath4
Dim strDriveLetter1, strDriveLetter2, strDriveLetter3, strDriveLetter4, bUpdateProfile, bForce
Dim WshNetwork, fs

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set fs = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")

bForce = "True"
bUpdateProfile = "True"

strDriveLetter1 = "H:"
strDriveLetter2 = "I:"
strDriveLetter3 = "S:"
strDriveLetter4 = "U:"

strRemotePath1 = "\\serverName\shareName"
strRemotePath2 = "\\serverName\shareName"
strRemotePath3 = "\\serverName\shareName"
strRemotePath4 = "\\serverName\shareName"

'Section which remove the drives:
If fs.DriveExists(strDriveLetter1) Then WshNetwork.RemoveNetworkDrive strDriveLetter1, bForce, bUpdateProfile End If
If fs.DriveExists(strDriveLetter2) Then WshNetwork.RemoveNetworkDrive strDriveLetter2, bForce, bUpdateProfile End If
If fs.DriveExists(strDriveLetter3) Then WshNetwork.RemoveNetworkDrive strDriveLetter3, bForce, bUpdateProfile End If
If fs.DriveExists(strDriveLetter4) Then WshNetwork.RemoveNetworkDrive strDriveLetter4, bForce, bUpdateProfile End If

'Section which maps drives:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1, bUpdateProfile
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2, bUpdateProfile
objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3, bUpdateProfile
objNetwork.MapNetworkDrive strDriveLetter4, strRemotePath4, bUpdateProfile

' Extra code just to add a message box
' WScript.Echo "Map drives " & strDriveLetter1 & strDriveLetter2 & strDriveLetter3 & " & " & strDriveLetter4

'End of Windows Logon Script

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Advanced VBScript for Microsoft Windows Administrators Microsoft WSH and VBScript Programming for the Absolute Beginner