Solved

Script to Map Network Drives

Posted on 2013-01-24
8
681 Views
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.

thanks,
darin
0
Comment
Question by:DarinOBrien
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 5

Accepted Solution

by:
Harsem earned 350 total points
ID: 38816593
Hello,

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

Jens
0
 
LVL 26

Expert Comment

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


The syntax of this command is:

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

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

NET USE [/PERSISTENT:{YES | NO}]


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

0
 

Author Comment

by:DarinOBrien
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?

Thanks!
darin
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Assisted Solution

by:beefstu123
beefstu123 earned 50 total points
ID: 38816714
Hi,
here is another way to do it using IP address:

EG:

net use G: \\192.168.0.121\[folder name]

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

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

EG:

net use G: \\192.168.0.121\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: \\192.168.0.121
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
beefstu123
0
 
LVL 5

Assisted Solution

by:Harsem
Harsem earned 350 total points
ID: 38816746
Hello,

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

Jens
0
 
LVL 26

Expert Comment

by:pony10us
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.
0
 
LVL 77

Assisted Solution

by:Rob Williams
Rob Williams earned 100 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:
http://blog.lan-tech.ca/2012/03/01/dive-mapping-basics/
0
 

Author Closing Comment

by:DarinOBrien
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.

darin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

775 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