Network drive mapping from VBScript

4.5
(2)

Mapping Windows Network Drive using Visual Basic Script

VBScript netdrive.vbs for Network drive Mapping

VBScript can be used to connect network drives to Windows shares on servers or NAS devices. In situations where batch processing is not the right choice, or group policy is not the application you want, a Visual Basic Script can meet the requirement, for example, to apply login scripts to VPN Remote Clients who are not members of the AD domain.

Microsoft VBScript contains object classes, methods (functions / procedures) and value structures. Our MapNetworkDrive object is used as a method here. The method or verb in turn manipulates values.

Any object can be used, the object objNetwork is defined here, script developers like to stick to patterns and their variables. A prefix of str indicates a string value, while the obj prefix indicates an object. After WScript has created our objNetwork, it can be edited using the MapNetworkDrive method.

VBScript for Network drive Mapping

The following VBScript netdrive.vbs as an example, assign the network mapping to drive Z: for a specific user; the login can be performed as a different user than the one with whom you have authenticated yourself on the client.

Option Explicit
Dim objNetwork, strDriveLetter, strRemotePath, strUser, strPassword, strProfile, WshShell

' Set credentials & network share to variables.
strDriveLetter = "Z:"
strRemotePath = "\\server\share"
strUser = "domain\username"
strPassword = "topsecret"
strProfile = "false"

' Create a network object (objNetwork) do apply MapNetworkDrive Z:
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword

' Open message box, enable remove the apostrophe at the beginning.
' WScript.Echo "Map Network Drive " & strDriveLetter
MsgBox " Explorer launch Network Drive " & strDriveLetter, vbInformation, "Network Drive Mapping"
' Explorer will open the mapped network drive.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "explorer.exe /e," & strDriveLetter, 1, false
WScript.Quit

The corresponding variable assigned line 5 – 9 in which the placeholder is defined between quotation marks.

Assign VBScript values to variables

  1. For strDriveLetter choose a desired network drive letter.
  2. For strRemotePath add the UNC path to the network share.
  3. For strUser add the user name. If this is a member of an AD domain, the domain prefix must be given, using like domain\user.
  4. For strPassword add the users password.

Run VBScript WSH from batch

If a batch is already used, for example netlogon.bat, then from the batch file the VBScript netdrive.vbs is executed as follows:

start /wait "" cscript //Nologo netdrive.vbs
VBScript Network Drive Mapping

If you do not want to output Visual Basic Console Screen during execution, use option /B to start the application without opening a new window.

start /B /wait "" cscript //Nologo netdrive.vbs

After successfully logging in, the network drive for the Windows network share is created and then opened in Explorer.

How useful was this post?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *