Map network drive with a visual basic script

codeEvery now and again, I have had requirements to access shared drives on other domains on which I have an account and do not want to create a permanent network connection. A couple of years ago, I stumbled upon this visual basic script that I have been using without any issues. I have tested this script using Microsoft Windows XP with SP2 and SP3 successfully and repeatedly.

MapNetworkDrive.vbs code

Option Explicit

Dim objNetwork, objShell, objExp
Dim objFSO, objFileCopy
Dim strMapDriveLetter, strMapDrivePath, strUserName, strPassword, bUpdateProfile
Dim strSourceLetter, strSourcePath, strFullSource
Dim strTargetLetter, strTargetPath, strFullTargetPath, strFullTarget
Dim CheckDrive, AlreadyConnected, intDrive

Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' Init variables
' --------------

'set drive letter to map
strMapDriveLetter = "m:"

'map drive to this path
strMapDrivePath = \\yourservername\share

'set security info
strUserName = "domain\useracct"
strPassword = "plaintextpasswordhere"
bUpdateProfile = "false"

' Run procedure
' -------------

'check if network drives already exist and remove
Call DeleteDrive

'map network drives
objNetwork.MapNetworkDrive strMapDriveLetter, strMapDrivePath, bUpdateProfile, strUserName, strPassword

' Calls subroutine to check if drive is already connected
'call Explorer

'WSCript.Echo "End Script"

Sub DeleteDrive()
On Error Resume Next
AlreadyConnected = False

'For intDrive = 0 To CheckDrive.Count - 1 Step 2
For intDrive = 0 To CheckDrive.Count
If CheckDrive.Item(intDrive) =strMapDriveLetter Then AlreadyConnected =True

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strMapDriveLetter, True, True
'Msgbox "Drive Deleted"
End if
End Sub

sub Explorer()
set objExp = CreateObject("WScript.Shell")
objExp.Run ("Explorer" & " " & strMapDriveLetter )
End Sub