Mapping Network Drives with VBS

If you need to programatically map Windows Network drives you can do it in two ways. The quick and easy way is of course to use the net command:

net use x: \\host\share

I use this method routinely for startup scripts. It works great if you are joined to the domain, and have permissions to access the share in question.

But sometimes you may want to do more. For example, you may want to create a script which will prompt a user from username and password with a nice dialog box and then attempt to map a share on a remote domain using these credentials. Sure, you can still attempt to do that via batch file, but users prefer GUI’s. This is where VBS comes in handy.

Here is a very simple script that will perform the mapping:

Dim mapper
Set mapper = WScript.CreateObject("WScript.Network")
mapper.MapNetworkDrive "x:", "\\host\share", true, "DOMAIN\user", "password"

The two first parameters are required, while the rest is optional. Most of them should be self explanatory. If the third parameter is set to true the mapping is persistent. Else, it will disappear after the machine is rebooted.

You can remove the drive just as easily:

mapper.RemoveNetworkDrive "x:", true, true

Only the first parameter is required. Setting the second parameter to true tells windows that you want to disconnect the drive even if it is in use. Last parameter is equivalent to the persistence parameter in the MapNetworkDrive function. If you set it to false the mapping will reappear after the system is rebooted.

[tags]vbs, network drives, map network drives, windows[/tags]

This entry was posted in programming and tagged , . Bookmark the permalink.



Leave a Reply

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