Reconnect mapped network drives using Command Prompt batch script

When booting into Windows, the OS sometimes does not reconnect mapped network drives even though the mapping is configured with the “Reconnect at sign-in” option. This is due to various timing constraints of resources during boot.

Although there are free Third Party software available that can automatically reconnect your mapped network drives, I would rather be able to do this using built-in tools than add more unnecessary software to my system. I’ve therefore written a Command Prompt batch script using the “NET USE” command which runs in Windows 10 Pro 64-bit (ver 1909).

The script is saved as a .CMD batch file which is executed twice by the Task Scheduler at user log on; one in non-admin and the other in admin mode (run in highest privilages). This allows a mapped drive to be visible to those applications running in non-admin and also to those applications running in admin modes. I also used the CMDKEY at the Command Prompt (not in a batch script) to add my network share credentials so that I didn’t have to add the credentials to the NET USE command. The CMDKEY format was:

cmdkey /add:192.168.49.69 /user:username /pass:password

The batch script is below. Note that this post is an update of a previous post which had some flaws.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET me=%~n0
SET parent=%~dp0

TITLE Mapping network drive
@ECHO Please wait whilst we connect your drive
TIMEOUT /t 1 /NOBREAK >NUL

:Start
ECHO _____________________________________

IF EXIST D:\NUL (
    ECHO:
    ECHO D: drive is already mapped
    GOTO Endofscript
) ELSE (
    ECHO:
    ECHO D: drive does not exist
)

IF EXIST \\192.168.49.69\Data\ (
    ECHO:
    ECHO Network folder exists so run NET USE command
    NET USE D: \\192.168.49.69\Data /PERSISTENT:YES
) ELSE (
    ECHO:
    ECHO Network folder does not exist
    GoTo ErrorPrompt
)

IF "%ERRORLEVEL%" NEQ "0" (
    IF "%ERRORLEVEL%" NEQ "2" (
        ECHO:
        ECHO NET USE returncode has error and is %ERRORLEVEL%
        GOTO ErrorPrompt
    )
) ELSE (
    ECHO:
    ECHO NET USE returncode should be 0 and is %ERRORLEVEL%
)

REM catchall
GOTO Endofscript

:ErrorPrompt
ECHO _____________________________________
ECHO:
SET /P userinput="Errors were found. Do you wish to try again [Y/n] "
IF /I "%userinput%" EQU "y" GOTO Start
IF /I "%userinput%" EQU "" GOTO Start
IF /I "%userinput%" EQU "n" GOTO Endofscript
GOTO ErrorPrompt

:Endofscript
ECHO _____________________________________
ECHO:
@ECHO Please wait, script is closing.
TIMEOUT /t 5 /NOBREAK >NUL
ENDLOCAL
@ECHO OFF
@EXIT /B 0

Reconnect mapped network drives using Windows Command Prompt script

I have an updated post at https://www.silentnomad.com/blog/2020/01/26/1885/


When booting into Windows, the OS sometimes does not reconnect mapped network drives even though the mapping is configured with the “Reconnect at sign-in” option. This is due with various timing constraints of resources during boot.

Although there are free Third Party software available that can automatically reconnect your mapped network drives, I would rather be able to do this using built-in tools than add more unnecessary software to my system. I’ve therefore written a Command Prompt script using the “net use” command. Not the prettiest of code but it does the job. Tested in Windows 10 Pro 64-bit.

The script was saved as a .CMD batch file which is executed by twice using Task Scheduler at user log on; one in non-admin and the other in admin mode. This allows mapped drives to be visible to those applications running in non-admin and also to those applications running in admin modes.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET me=%~n0
SET parent=%~dp0
TITLE Mapping network drives
@ECHO Please wait whilst we reconnect your network drives.
SET neterror=0
SET counter=1
SET loopvalue=11
SET /A trueloop=%loopvalue%-1
:Start
ECHO _____________________________________
ECHO:
IF "%counter%" EQU "%loopvalue%" (
ECHO Connection to D: drive timed-out
SET neterror=1
GOTO End
)
ECHO Attempt %counter% of %trueloop% to D: drive
TIMEOUT /t 5 /NOBREAK >NUL
IF EXIST D:NUL (
ECHO Attempt %counter% successful.
GOTO End
)
NET USE D: \192.168.49.69Data /PERSISTENT:YES
IF "%ERRORLEVEL%" NEQ "0" IF "%ERRORLEVEL%" NEQ "85" (
SET /A counter=%counter%+1
GOTO Start
)
:End
IF "%neterror%" EQU "0" GOTO Endofscript
:userconfirm
ECHO _____________________________________
ECHO:
SET /P userinput="Errors were found. Do you wish to try again [Y/n] "
IF /I "%userinput%" EQU "y" GOTO Start
IF /I "%userinput%" EQU "" GOTO Start
IF /I "%userinput%" EQU "n" GOTO Endofscript
GOTO userconfirm
:Endofscript
ECHO _____________________________________
ECHO:
@ECHO Please wait, script is closing.
TIMEOUT /t 5 /NOBREAK >NUL
ENDLOCAL
@ECHO OFF
@EXIT /B 0