Tuesday, August 20, 2013

Windows 7 Drive Mapping Using Batch Command

During the Windows XP era, it used to be easy to map a drive letter to a local folder or network shared directory. But since Windows Vista/7, with it's better security controls (e.g. UAC control), it seems to require a little more steps to achieve that. The following examples show how to make a proper mapping in Windows Vista/7.

This example assumes that I need to map to 3 shared directories ("MyShare1, "MyShare2", "MyShare3") in a server named "MyServer", to driver letters "H", "I", "J" respectively, with the login credential as user "weizh" and password "myPassw0rd".

To map network shared directories:

1. Create a batch file named "map_myserver_user.bat" with the following content:
REM ==== Maps to network drives from MyServer.

NET USE H: \\MyServer\MyShare1 /USER:weizh myPassw0rd /PERSISTENT:NO
NET USE I: \\MyServer\MyShare2 /USER:weizh myPassw0rd /PERSISTENT:NO
NET USE J: \\MyServer\MyShare3 /USER:weizh myPassw0rd /PERSISTENT:NO

2. Create a shortcut for "map_myserver_user.bat" (right-click on the file > Send to > Desktop (create shortcut)), and rename the shortcut as "map_myserver_admin.bat".

3. Right-click on the "map_myserver_admin.bat" shortcut file > Go to Properties > Shortcut > Click the "Advance" button > Tick "Run as administrator" > Click OK.

4. By now you will have 2 files with identical contents (1 batch file and 1 shortcut of the batch file). To start mapping the shared directories to the drive letters, just run these batch files in the following order:
  • "map_myserver_admin.bat"
  • "map_myserver_user.bat"

5. After the batch files are executed, the mapped drives will appear in your Windows Explorer.

Note: The batch files must be run BEFORE you manually accessing the network drive (e.g. typing "\\MyServer\MyShare1" in the address bar) or manually key-in the user credential, after each Windows startup.

To disconnect network shared directories:

1. Follow the same steps as above, but this time save the batch file and shortcut of the batch file with different names (e.g. "map_myserver_disconnect_user.bat", "map_myserver_disconnect_admin.bat"), and changed the content as follows:
REM ==== Disconnect mapped network drives from MyServer.


2. To disconnect the shared directories from the drive letters, just run the batch files in the following order:
  • "map_myserver_disconnect_admin.bat"
  • "map_myserver_disconnect_user.bat"

Of course, the same methods also can be used for mapping/disconnecting to/from local directories. As a reference, the following examples show the content of the batch files to map a local directory "D:\Data\MyFolder" to drive letter "P":

To map a local directory:
SUBST P: D:\Data\MyFolder

To disconnect a local directory:

If you find this post helpful, would you buy me a coffee?

No comments:

Post a Comment