Hack 48 Map Network Drives


This quick way to map a network drive can
replace the traditional approach of using batch files.
Using VBScript, you can easily map drive letters to shared folders on your
network. This approach allows you to use VBScript to map and unmap
network drivesfor example, in logon scripts. It also allows
you greater flexibility in customizing scripts to perform actions
across a network and doesn't require the
net use command to work.
Basically, the script creates the Network
scripting object and then uses the MapNetworkDrive
method to assign a drive letter to a network share.
I've included examples of code for both mapping and
unmapping network drives.
The Code
First, here's the code for mapping a network drive:
Dim net
Set net = CreateObject("WScript.Network")
net.MapNetworkDrive "Z:", "\\server\share"
And here's code for unmapping a network drive:
Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemoveNetworkDrive "Z:"
Running the Hack
To use the first snippet of code, type it into Notepad (with Word
Wrap disabled) and save it with a .vbs
extensionfor example, as
map.vbs. Then modify this line to specify the
drive letter and share you want to map:
net.MapNetworkDrive "Z:", "\\server\share"
For example, to map the drive letter K: to the
Sysback share on a file server with an IP
address of 172.16.11.230, change the line to:
net.MapNetworkDrive "K:", "\\172.16.11.230\Sysback"
Then, run the script either by creating a shortcut to it and
double-clicking on the shortcut, by opening a command prompt and
typing cscript.exe map.vbs, or
by calling it from a batch file using a line like this (where
path is the absolute path to where the
script is located):
cscript //nologo path\map.vbs
To unmap this drive, type the second code snippet into Notepad, save
it as unmap.vbs, and change this line:
WshNetwork.RemoveNetworkDrive "Z:"
to this:
WshNetwork.RemoveNetworkDrive "K:"
Then, run the script using any of the methods described previously.
Rod Trent