Map network drives at login

December 27th, 2007

(Win2k, XP, Vista) Using NET USE in a batch file at login to force-connect mapped drives... useful for situations where mapped drives mysteriously disappear (this script can safely be re-run repeatedly) or to prevent conflicts between network shares requiring conflicting credentials.

First, copy and paste the following into a new text file, and save it somewhere convenient as MapNetworkDrives.bat:

@echo off
rem Windows 2k/XP/Vista batch file

rem Designed to be run at logon, after scheduled interval, or on demand, to map
rem network drives with specific permissions

rem -- SYNTAX

rem Pause for timed interval:
rem ping 127.0.0.1 -n NUMBEROFPINGS -w PINGTIMEOUT > nul

rem Remove existing share:
rem net use DRIVELETTER: /delete /y

rem Add new share:
rem net use DRIVELETTER: \\SERVER\SHARENAME
rem        [/user:SERVER\USERNAME PASSWORD] [/persistent:YES|NO]

TITLE Connecting network drives...
echo.
echo ____________________________________________
echo.
echo   Connecting network drives manually
echo ____________________________________________
echo.
echo.

rem Wait 5 seconds to allow network to become available
rem in case this was run at login.
rem If this script is never run at login, this section can be
rem commented out or removed.
echo - Waiting 5 seconds for network...
ping 127.0.0.1 -n 2 -w 1000 > nul
ping 127.0.0.1 -n 5 -w 1000 > nul
echo Now it should be safe to try connecting.
echo.
echo.

rem First disconnect existing maps to ensure permissions don't clash
echo - Removing any existing mapped drives...
if exist p: net use p: /delete /y
if exist q: net use q: /delete /y
if exist r: net use r: /delete /y
echo.
echo.

rem Then connect shares with full permissions
echo - Connecting new mapped drives...
net use p: \\server\share1 /user:"server\priviledged" secret /persistent:no
net use q: \\server\share2 /user:"server\priviledged" secret /persistent:no
net use r: \\server\share3 /user:"server\priviledged" secret /persistent:no
echo.
echo.

TITLE Finished
echo ____________________________________________
echo.

rem Wait another 5 seconds
echo This window will close in 5 seconds...
ping 127.0.0.1 -n 2 -w 1000 > nul
ping 127.0.0.1 -n 5 -w 1000 > nul

Of course, replace "server" with your server name, "share#" with your share name, "privileged" with a username on the server with appropriate access privileges for the share, and "secret" with the password for the above username.

Simply create a shortcut to this batch file in the Start Menu under the Startup folder, right-click Properties and set Run as Minimized, and it should stay out of the way while safely doing its thing.

Note that this script doesn't always work successfully if there is an open network share using different credentials than those supplied... NET USE /DELETE seems to throw an error in this case rather than forcing the connection to close. Just manually disconnect any remaining shares to prevent them from reconnecting automatically at the next login, reboot, and run the script then.

Add Your Comment

Note: all fields are optional