Windows Logon Script – MapNetworkDrive to UserName

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.

On the other hand, if you want a script to actually create the user’s folder, then check here.

Topics for VBS UserName

 ♦

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\2011

Example 1 – MapNetworkDrive to UserNameMapNetworkDrive to user's home directory

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 guyt, hence you see in Windows Explorer:
guyt on ‘grand\home'(J:).

Pre-requisites

  1. On Line 15 change the server name from ‘\\grand’ to your server name.
  2. Make sure that your server has a share called ‘\home’.
  3. 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

  1. Copy and paste the VBS username script below into notepad.
  2. Change the server name from "\\grand to the name of your server.
  3. Save the file with .vbs extension e.g. MNDUserName.vbs.
  4. 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 Guy Thomas https://computerperformance.co.uk/
‘ 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.

Note: If you want a script which will actually create the home folders, check here.

Guy Recommends SolarWinds’ Free Network MonitorSolarwinds Network Device Monitor

Thus utility makes it easy to check the health of a router or firewall.  Check the real-time performance, and availability statistics, for any device on your network.  Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl – it’s free. Download your free Network Device Monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

Example 2 – MapNetworkDrive Variations

This logon script does exactly the same job as Example 1.  What I want to highlight here is the importance of attention to detail, in particular constructing the path to the user’s folder using ‘\’.

‘ MNDUserName2.vbs
‘ VBScript to map a network drive to the UserName.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.1 – April 24th 2010
‘ —————————————————-‘
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "K:"
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

‘ Extra code just to add a message box
WScript.Echo " Launch Windows Explorer, check: "& strDriveLetter
WScript.Quit

‘ End of Example of MapNetworkDrive VBScript.

VBS Learning Points

Note 1:  Can you see how I control the troublesome "\" by using: strRemotePath = "\\grand\home\"  (Not strRemotePath = "\\grand\home" )  If harping on about the slash prevents you making a mistake, then I will feel justified in showing you this second method.

Note 2:  I deliberately changed the drive letter from J: to K: so that it would not interfere with the Example 1 logon script.

Note: If you want a script which will actually create the home folders, check here.

Guy Recommends: Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try – it’s free!

Download Permissions Analyser – Free Active Directory Tool

Example 3 – 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.

  1. On Line 10 change the server name from ‘\\grand’ to your server name.
  2. Make sure that your server has a share called ‘\home’.

Instructions to MapNetworkDrive

  1. Copy and paste the VBS username script below into notepad.
  2. Change the server name from "\\grand to the name of your server.
  3. Save the file with .vbs extension e.g. MNDUserNameBonus.vbs.
  4. 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 Guy Thomas https://computerperformance.co.uk/
‘ 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.

Learning Points

Note 1:  Observe the Sub ShowExplorer().  As a change from a WScript.Echo, here is a script which finishes by opening Windows Explorer.  While the sub routine is at the very end of the script it is accessed via the Call ShowExplorer earlier in the script.

Employ PowerShell for Your Logon Scripts

 Instead of VBScripts you could use PowerShell to create logon scripts.  Although PowerShell is used mainly for configuring the operating system, it’s possible to build ComObjects to MapNetworkDrive. The technique involves creating a wrapper around familiar VBScript commands. Here is example of PowerShell’s New-Object cmdlet manipulating MapNetworkDrive:

# PowerShell Logon Script Example
$PSnet = $(New-Object -ComObject WScript.Network)
$PSnet.MapNetworkDrive("H:", "\\BigServer\Stuff")

You could save these instructions in a .ps1 file.  However, the hard part is executing  this .ps1 file as a logon script.  See more about PowerShell and logon scripts.

Summary – MapNetworkDrive to UserName VBS

Being able to MapNetworkDrive to a subfolder extends the capability of Microsoft’s VBScript.  The secret is to understand where to place the extra slash ‘\’ which tells the script where find the subfolders.  Another important scripting principle, is to master the basic logon script before attempting this more difficult example of multiple map network drives.

 

Download my Logon Script eBook for only $6.25

Logon ScriptThe extra features you get in your eBook include, more pages full of detailed examples.  Also, ten ‘how to…’ sections, with screen shots showing which menus to use.  Go for Guy’s eBook – and get a printable version with copy enabled and no expiry date.

  Jumbo Script 7 Package

If you like this page then please share it with your friends

 


See more logon scripts examples

Logon Script Home   •EnumNetworkDrives   • Map Network Drive Group   • Free Import CSV Tool

Map Network Drive Script   • Vbscript Map Network Drive Username   • Map Multiple Network Drives

ObjNetwork.MapNetworkDrive   • Disconnect Network Drives   • Logon script group policy