Introduction to Map Network Drive - UserName
This page has a specific goal, to map a network drive to a UserName, rather than the root of the \home share. In many ways this is the classic logon script task, each user has an individual folder on the Window 2003 server, and you want the script to map to a different folder for each user. I recommend that you master the simple MapNetworkDrive script, then graduate to this UserName VBScript.
Mapping to UserName Scenario
Suppose you want users to have their J:\ (home drive) mapped, not to the root share, but to a sub directory corresponding to their individual UserName. Here is how you script MapNetworkDrive. Instead of connecting to Windows Server 2003 here:
\\ server\home
You prefer to MapNetworkDrive to a subfolder which is one level further down:
\\ server\home\UserName
Another possible scenario is where you want to map to subfolder on a normal, non-home directory share. For example, instead of connecting to a Windows server here:
\\ BigServer\reports
You prefer to MapNetworkDrive to a subfolder one level further down:
\\ BigServer\reports\2010
Suppose you want users to have their J:\ (home drive) mapped, not to the root share, but to a sub directory corresponding to their individual UserName. Here is how you script MapNetworkDrive. Instead of connecting to Windows Server 2003 here:
\\ server\home
You prefer to MapNetworkDrive to a subfolder which is one level further down:
\\ server\home\UserName
Another possible scenario is where you want to map to subfolder on a normal, non-home directory share. For example, instead of connecting to a Windows server here:
\\ BigServer\reports
You prefer to MapNetworkDrive to a subfolder one level further down:
\\ BigServer\reports\2010
Example 1 - MapNetworkDrive to UserName
Our objective is to map the J: to a folder called '\home\UserName' on a server called '\\grand'. My advice is to build up your logon scripts gradually, you may consider getting the simple MapNetworkDrive script working first. In my example opposite, the user's name is Wserevi, hence you see in Windows Explorer:
Wserevi on 'grand\home'(J:).
Pre-requisites
Instructions to MapNetworkDrive
Wserevi on 'grand\home'(J:).
Pre-requisites
- On Line 15 change the server name from '\\grand' to your server name.
- Make sure that your server has a share called '\home'.
- Underneath '\home', you have already created a folder which matches the name of the user you are testing. Moreover, the name of this folder matches exactly, the users logon name.
Instructions to MapNetworkDrive
- Copy and paste the VBS username script below into notepad.
- Change the server name from "\\grand to the name of your server.
- Save the file with .vbs extension e.g. MNDUserName.vbs.
- Double click your logon script and check in your Windows Explorer for a new drive called : UserName on 'grand' (J:)
MNDUserName.vbs
' VBScript to map a network drive to the UserName.
' Author Rukshar Khan http://fijitechie.weebly.com/
' Version 1.3 - April 24th 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "J:"
strRemotePath = "\\grand\home"
' Purpose of script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result J: drive
Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName
strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _
& "\" & strUserName
' Extra code just to add a message box
WScript.Echo " Launch Explorer, check: "& strDriveLetter
WScript.Quit
' End of MapNetworkDrive Example logon script.
' VBScript to map a network drive to the UserName.
' Author Rukshar Khan http://fijitechie.weebly.com/
' Version 1.3 - April 24th 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "J:"
strRemotePath = "\\grand\home"
' Purpose of script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result J: drive
Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName
strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _
& "\" & strUserName
' Extra code just to add a message box
WScript.Echo " Launch Explorer, check: "& strDriveLetter
WScript.Quit
' End of MapNetworkDrive Example logon script.
Learning Points
Note 1: The script proper begins by creating a network object thus:
Set objNetwork = WScript.CreateObject("WScript.Network"). This emphasises the role of the WScript in creating and hosting the object, which go on to manipulate the network drive. In fact the first WScript is optional, even redundant this works just as well:
Set objNetwork = CreateObject("WScript.Network"). To me these tiny differences give perspective and appreciation to what the command is achieving.
Note 2: As line 17 was so long, I needed the _ (underscore), which joins the two lines when VBScript processes the command. Without the underscore the code does not work, VBScript believes you have two lines because it does not understand word-wrap.
Note 3: See how the script cleverly joins, or concatenates the strUserName to the strRemotePath. (My) VBScripts use lots of & (ampersands) to concatenate variables.
Note 4: The tiniest but most troublesome part of this script is the slash "\" after home. Always remember that if your mapping needs extra subfolders, then put in another "\" between each folder level. In scripting terms this is a small point, but in practical terms it's so vital that I have another example showing a different way of achieving the same goal, namely to map to the UserName folder.
Set objNetwork = WScript.CreateObject("WScript.Network"). This emphasises the role of the WScript in creating and hosting the object, which go on to manipulate the network drive. In fact the first WScript is optional, even redundant this works just as well:
Set objNetwork = CreateObject("WScript.Network"). To me these tiny differences give perspective and appreciation to what the command is achieving.
Note 2: As line 17 was so long, I needed the _ (underscore), which joins the two lines when VBScript processes the command. Without the underscore the code does not work, VBScript believes you have two lines because it does not understand word-wrap.
Note 3: See how the script cleverly joins, or concatenates the strUserName to the strRemotePath. (My) VBScripts use lots of & (ampersands) to concatenate variables.
Note 4: The tiniest but most troublesome part of this script is the slash "\" after home. Always remember that if your mapping needs extra subfolders, then put in another "\" between each folder level. In scripting terms this is a small point, but in practical terms it's so vital that I have another example showing a different way of achieving the same goal, namely to map to the UserName folder.
Bonus Script: MapNetworkDrive with Windows Explorer
Here is a bonus script. It introduces two features, a sub routine, and instead of a message box, objShell creates an instance of Windows Explorer to display the mapped network drive. Naturally, this sub routine is not needed in a production script. Pre-requisites.
- On Line 10 change the server name from '\\grand' to your server name.
- Make sure that your server has a share called '\home'.
- Copy and paste the VBS username script below into notepad.
- Change the server name from "\\grand to the name of your server.
- Save the file with .vbs extension e.g. MNDUserNameBonus.vbs.
- Double click and check observe your Windows Explorer open the new mapped X: drive
' MNDUserNameBonus.vbs
' VBScript map a network drive to the UserName. And open Explorer
' Author Rukshar Khan http://Fiji Techie.weebly.com/
' Version 2.2 - September 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork, objShell
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "X:"
strRemotePath = "\\grand\home\"
' Purpose of the script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result K: drive
Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName
strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath & strUserName
' Bonus code to open Explorer at the mapped drive letter
Call ShowExplorer
WScript.Quit
Sub ShowExplorer()
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" & " " & strDriveLetter & "\" )
End Sub
' End of Example of MapNetworkDrive logon script.
' VBScript map a network drive to the UserName. And open Explorer
' Author Rukshar Khan http://Fiji Techie.weebly.com/
' Version 2.2 - September 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork, objShell
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "X:"
strRemotePath = "\\grand\home\"
' Purpose of the script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result K: drive
Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName
strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath & strUserName
' Bonus code to open Explorer at the mapped drive letter
Call ShowExplorer
WScript.Quit
Sub ShowExplorer()
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" & " " & strDriveLetter & "\" )
End Sub
' End of Example of MapNetworkDrive logon script.