Script to Map Network Drives

Posted on 2013-01-24
Medium Priority
Last Modified: 2013-02-11
I'm trying to write a batch file that will automatically map network drives upon restart of a number of workstations running Windows7, Vista, or XP.  

There are lots of postings on this topic in EE and elsewhere.  It appears very straightforward, although I have absolutely no prior experience with this stuff.  See the following as an example:  http://community.spiceworks.com/how_to/show/1487-how-to-create-a-batch-file-for-network-drive-mapping.

I followed the instructions above:
1.  Open notepad.
2.  Typed the following:  net use G:\\servername\D
3.  Saved file as a .bat (NOT a .txt file)

To test it, I simply saved it on my desktop and double-clicked it to run it.  I expected it to create a mapped network drive with letter designation "G".  It doesn't appear that it did anything.

I've tried running it on both Windows 7 and Vista.  I've also tried running it as administrator.  No joy.

Ultimately, I would like this batch file to map multiple drives from all the computers in my office.  

Any help would be greatly appreciated.

Question by:DarinOBrien
  • 2
  • 2
  • 2
  • +2

Accepted Solution

Harsem earned 1400 total points
ID: 38816593

that syntax is almost correct - and you also have to make sure that the drive is shared. Windows has a number of default administrative shares, but they are hidden.

To access these administrative shares you can use:

net use G: \\servername\D$
(note the space between  the ":" & "\\")

However I think it would be better if you shared a folder on the server (Servername) and then you can access it via:

net use G: \\servername\Folder

Hope this helps

LVL 26

Expert Comment

ID: 38816658
Microsoft is good at giving help for commands:

The syntax of this command is:

[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME


As for a batch file you are correct to save it as a ".bat"

@echo off
net use g: \\servername\Folder /persistent:yes

Open in new window


Author Comment

ID: 38816696
The space between the ":" and the "\\" was the problem!

I'm actually trying to map a drive to a shared folder on the server ("D").  It's not hidden so I assume it's not an administrative share?
Can you also please tell me how to make this batch file first delete ALL mapped network drives that might be on the workstation?  Alternatively, it would probably work to delete any existing shares with the same letter designation (e.g. I want to create a new share "G" so I want to delete and pre-existing shares "G").

Lastly, can you tell me where to place this batch file so that it runs automatically everytime the users restart their computers?  Again, working with XP, Vista and Windows7 so maybe they're different locations?

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Assisted Solution

beefstu123 earned 200 total points
ID: 38816714
here is another way to do it using IP address:


net use G: \\\[folder name]

also does the server have a password if it does you have to apply it to your bat file:

net use G: \\\[folder name] /user:[username] [password]
Note there is a space between the username and the password


net use G: \\\folder1 /user:admin1 user1
admin1 is the username and user1 would be the password

and adding another location is just adding another line to your bat file, save and test.
To check if your folders are shared just type the server ip address in to a folder address bar
EG: \\
and it will show what you have shared on that system
Note: you will have to setup static IP addresses for the server and any other system that are hosting the shared files if you dont' you will have to get the ip address every time the server restarts, to resolve this use Harsem's method and use the system name instead of the ip address.

Once that is done all you have to do is to add it to start up folder and it will run every time you system starts up (put a copy on the desktop as well sometimes the network hasn't been detected when start up tries to run the bat file)

hope it works

Assisted Solution

Harsem earned 1400 total points
ID: 38816746

to remove a share is easy:
net use /d g:

I do not know how to delete all, but that will remove G:

To automatically run that is easy:
(from www.computerhope.com/issues/ch000322.htm )  
Windows 98, XP, NT, 2000, Vista and later users

    Create a shortcut to the batch file.
    Once the shortcut has been created right-click the file and select Cut.
    Click Start, Programs, right-click the Startup folder and click Open
    Once the Startup folder has been opened click Edit and paste the shortcut into the startup. Any shortcuts in the startup folder will automatically start each time Windows starts.

Hope this helps

LVL 26

Expert Comment

ID: 38816869
If the computers are on a Active Directory domain then you could place the commands in the login script or push via GPO.
LVL 78

Assisted Solution

by:Rob Williams
Rob Williams earned 400 total points
ID: 38816903
A good idea is to disable persistent mappings and delete all existing mappings before creating new mappings to assure there are no conflicts with exiting mappings or ones created by users.  To do so use something similar to:

Net Use /persistent:no
Net Use * /delete
Net Use X: \\ServerName\ShareName1
Net Use Y: \\ServerName\ShareName2
Net Use Z: \\ServerName\ShareName3

You may also wish to review the following article as to how to apply the mappings, if you have a domain.  A more common practice today is group Policy preferences, but that is not 100% effective with XP machines.  The article also discusses other options:

Author Closing Comment

ID: 38877397
I used harsem solution first shown above:

net use G: \\servername\Folder

In combination with a bunch of these to delete all prior mapped network drives:

net use /delete X:

I avoided using IP addresses for fear that they would change.

Thanks to everyone who participated.  I especially appreciate the link to detailed reference material.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question