How To Create A Windows Batch File To Map Multiple Drives At Once

11

winlogo.jpgMapping a drive to a share on a remote computer can be a big time saver when you need to access files and folders remotely. Using Windows Explorer, you can easily map the drive to the share on a remote computer when needed.

But what if you need to map multiple drives at once, or you want to always map the drive when you logon to your computer?

The best solution is to create a batch file that you can click on, when you need to map the drives, or copy it to your Windows user account Startup folder so the drives are mapped automatically during logon.

To map drives using a batch file, we'll need to use the net use command. At it's simplest form, the command looks like this:

net use [devicename | *] [\\computername\sharename

where:

devicename = the dive letter for the map drive

computername = is the computeer wher the share exists

sharename = is the name of the share

So to create a batch file that will map a drive to different computers that are sharing folders, we'll use the following commands:

net use W: \\computer1\MP3

net use X: \\computer2\Photos

Note: you can asssign any drive letter you want, as long as it is not in use by your computer (for example A or C drives).


What About Authentication?

Using the commands above to map drives, assumes that the account you are logged on as, exists on the remote computer where the shared folder is located, or if you are at work, is part of a Windows Domain.

If you need to authenticate when mapping the drive, we'll need to use an account that exists on the remote computer or in a Windows Domain, and add it onto the net use command as shown below:

net use [devicename | *] [\\computername\sharename password  /USER:username

Therefore the command will look like this when used in a batch file:

net use W: \\computer1\MP3 mypassword /USER:mary

net use X: \\computer2\Photos mypassword /USER:bob

If an error occurs (unknown user name), you will need to use the /USER switch with the domainname option as follows:

net use W: \\computer1\MP3 mypassword /USER:mydomain\mary

If the remote computer is not part of a Domain, and is part of a Workgroup, then substitute the Domain for the name of the remote computer (where the user account resides).

net use W: \\computer1\MP3 mypassword /USER:computer1\mary


Creating The Batch File

Ok, now that we know how to use the net use command, let's create the batch file.

  • First, open up Notepad or your favorite text editor.
  • Next, copy the commands as shown below and paste them into the file. Don't forget to modify the net use command with the drive letter you want to use, the name of the Computer and share you want to map a drive to, and password and account name (if needed).

ECHO Begin Mapping Drives
net use W: \\computer1\MP3 mypassword /USER:mary
net use X: \\computer2\Photos myrealpassword /USER:bob

exit

  • Then save the file with any name and a .bat extension (make sure it's NOT saved using .txt as the file type extension), to a location where you can easily access it (such as your Desktop).
  • When you need to map the drive(s), just double click on it.


A Word About Security

Now, realize when using the net use command and hard coding passwords, make sure you understand that you are at risk if anyone gains access to the batch file. You don't want to create the batch file at work and save it to a File Server.

Use common sense, and don't be careless.

Comments on How To Create A Windows Batch File To Map Multiple Drives At Once Leave a Comment

January 7, 2009

Joel Potgieter @ 4:30 pm #

Another option is to use the PUSHD and POPD commands; but they differ in the way that it binds network paths:

If you specify a network path, the pushd command temporarily assigns the first unused drive letter (starting with Z: and working backwards) to the specified network resource. The command then changes the current drive and directory to the specified directory on the newly assigned drive. If you use the popd command with command extensions enabled, the popd command removes the drive-letter assignation created by pushd.

It will help if you understand how stacks work; as POPD will remove the most recently executed PUSHD drive letter.

They can also be used to bind drive letters to any path as well.

More info in the "Help and Support" sections of Windows accessed by hitting F1 and searching for PUSHD or POPD.

September 30, 2009

Steven @ 3:55 am #

Thanks a lot that helped me to mapped several drives to windows XP clients through domain policy in AD

October 14, 2009

Praveen @ 11:17 am #

Thanks a lot, the above blog came in really handy to resolve my issue. Keep up the good work. Thanks!

November 21, 2009

@neil @ 11:31 pm #

Can you please help me to create batch file for opening few application in some time interval and website with id and password directly after running a batch file.

Please need your help guys would be appreciate if some one descirbe with sample example

Thanks hips in advance

April 20, 2010

Brian.BOYOPE @ 2:33 am #

please sent me more detailed information on how to create windows batch file.
Thankyou..

April 27, 2010

k0k0pelli @ 2:28 pm #

All of this is great help. Mine is a little more. All machines are domain members, preparing for rollout, etc.
I can net use j:\computername\c$ manually in a command prompt window, and it will return asking for username and password. But if I have that same command set up in a batch file, with a set /p variable to request the machine name, it fails. The set /p variable is important so we can map to each machine as we have them side by side new/old and map to pull data.

For instance:
Echo mapping to old machine
echo "enter old machine name"
set /p computername=
net use j: \\%computername%\c$

I then get the answer that the network connection cannot be found.
When it gets to the net use line and the %computername% variable, it has the correct machine name from what you enter above from the set /p entry. It just won't complete the map.

May 13, 2010

Mo @ 5:48 pm #

If you're in a batch job, you have to say %%computername instead of %computername%

June 18, 2010

dominique @ 12:13 am #

Hello,

How to map the first available drive in a batch file …
net use …

Thanks,
Dom

k0k0pelli @ 8:17 am #

@Mo:

Interestingly enough, the roll out is all finished, and my answer turned out to be stupid syntax changes. The actual batch file looks like this, and worked perfectly!

echo enter "old machine name"
set /p "computername="
echo
net use z: "\\%computername%\c$" /user:%computername%\username password
net use z:
net use y: "\\%computername%\d$" /user:%computername%\username password
net use y:

In the example above, replace the word "username" with yours, or administrator, or whoever has rights. and the word "password" is also replaced to be the password corresponding to the username replaced. It was great how it worked, because from there, I had batch files created that used the mapping letters to copy data from z:, and from y:, to the correct locations on the new machine's C: and D: drives.

It works.

July 16, 2010

Roy @ 10:51 am #

As already noted, hard coded passwords are a bad idea.
A script can prompt the user for a password and hide what is entered.

I didn't invent this method. I've copied the relevant text from the link below.

From http://www.novell.com/communities/node/3124/using-ldap-fun-and-profit

Some batch file scripters add this little piece of code in their scripts:

:: Prompt for username and prompt for password using assembler code
@echo off
echo Enter the authorized LDAP username and password …
set /p authuser=Enter admin LDAP name:
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5x>in.com
set /p passwd=Enter password:in.com"

actually writes a little scrap of assembler code into an executable file in the current directory called "in.com". The following commands use it to capture the next keyboard input (the password) into the "passwd" variable without printing any characters to the screen. Then it deletes the scrap of code (del in.com) to tidy up.

July 27, 2011

Sowderman @ 5:21 pm #

I must be missing a subtle point in mapping multiples. I have multiple drives I want mapped and my .bat will map the first only, the rest remain disconnected. All of them are to the same resource with the same password just a folder name changing in the path. ANy help appreciated.

Leave a Comment

Fields marked by an asterisk (*) are required.

Subscribe without commenting