Categories :

Mapped Drives not reconnecting at startup – Windows 10, and Windows 11 – Solved

Last Updated on 30/01/2022 by Kevin Agar

Background… I’ve had problems for years where the mapped network drives would not reconnect during the startup of windows. I’ve searched the internet and found numerous articles explaining how to solve the issue. None of them worked for me.

Recently I started playing with Raspberry pi’s and set one up as a NEMS system to monitor my home network, plus a few sites like www.google.co.uk, www.virginmedia.com, www.kevinagar.uk so I could see if anything on my home network went down, or had issues, and if my broadband was suffering from downtime.

This created a Folder on the network called NEMS/Backup. I mapped this folder, so ended up with

Backup (\\NEMS) (X)

Every time I started Windows, this drive was mapped and connected without any errors.

This got me thinking about my NAS (Network Attached Storage) and why it wouldn’t stay connected, and if there was a way I could keep it connected. The NAS in question is an old DLink DNS320L.

I finally decided that if I disconnect the network drives during startup, and then connect the drives, it would work. So this is my method, whilst not very elegant, it worked. Copy the following text into a file and rename the file MapNetworkDrives.bat

:: Author Kevin B Agar
:: Date 3rd November 2021
:: Batch file to map network drives at startup
:: Create a log file
set LOGFILE=C:\logs\MapNetworkDrives.log
echo %date% %time% >> C:\logs\MapNetworkDrives.log
call :LOG >> %LOGFILE%
exit /B
:LOG
:: Remove any network drives
net use Y: /delete
net use Z: /delete
:: Map network drives
net use Y: “\192.168.0.13\Volume_1”
net use Z: “\192.168.0.13\My Documents”
:: Place the file in %ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp

The :: are comments.

set LOGFILE=C:\logs\MapNetworkDrives.log sets the location of the log file, change as required

call :LOG >> %LOGFILE% The >> causes the new entries into the log file to be added to the end. If you want to just have the latest information in the log file then just use a single >

net use Y: /delete Change this to the drive letter you want to unmap. Add as many drives as required.

net use Y: “\192.168.0.13\Volume_1” Change this to the drive letter you want to map the network drive to. You can change the IP address to the Network name, for example, net use Y: “\NETDRIVE\Volume_1”, and add as many drives as you want.

Save the file and place it in %ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp

This worked every time, but it flashed up a command window on the screen which wasn’t very pretty. To get around this I created a visual basic file to run the batch file and named it MapNetworkDrives.vbs.

Set WshShell = CreateObject(“WScript.Shell” )
WshShell.Run chr(34) & “C:\Scripts\MapNetworkDrives.bat” & Chr(34), 0
Set WshShell = Nothing

This file was place in %ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp and the file MapNetworkDrives.bat was placed in the folder C:\Scripts

Now every time the PC is started it runs the file MapNetworkDrives.vbs which calls the batch file MapNetworkDrives.bat which deletes all mapped drives, then maps all network drives and writes this into the log file, all silently

Having got this working, and because I like to tinker with things, I decided to limit the logfile size to 100Kb. This reduces the amount of scrolling needed to go to the end of the logfile. I added the following to the end of MapNetworkDrives.bat

@echo off
setlocal EnableExtensions DisableDelayedExpansion
:: Check the file size of log file and move it if its more than 100 Kb.
call :CheckLogSize “C:\logs\MapNetworkDrives.log” 102400
:: Other commands which append lines to log file.
endlocal
goto :EOF
:: The subroutine CheckLogSize must be called with two parameters: the log
:: file name without or with path as first parameter and the maximum file
:: size as second parameter.
:: Note: Windows command processor supports only 32 bit signed integers
:: which limits file size comparison to 2 GiB, i.e. 2147483647 bytes.
:: Therefore the second parameter should not be too large. It must be
:: made sure that the current file size of the log file is not exceeding
:: 2 GiB value on calling this subroutine. So better call this subroutine
:: once too often than once too rare.
:CheckLogSize
if not exist “%~1” goto :EOF
if %~z1 GTR %~2 move /Y “%~1” “%~dpn1_old%~x1” >nul
goto :EOF

The final batch file :-

:: Author Kevin B Agar
:: Date 3rd November 2021
:: Batch file to map network drives at startup
:: Create a log file
set LOGFILE=C:\logs\MapNetworkDrives.log
echo %date% %time% >> C:\logs\MapNetworkDrives.log
call :LOG >> %LOGFILE%
exit /B
:LOG
:: Remove any network drives
net use Y: /delete
net use Z: /delete
:: Map network drives
net use Y: “\192.168.0.13\Volume_1”
net use Z: “\192.168.0.13\My Documents”

@echo off
setlocal EnableExtensions DisableDelayedExpansion
:: Check the file size of log file and move it if its more than 100 Kb.
call :CheckLogSize “C:\logs\MapNetworkDrives.log” 102400
:: Other commands which append lines to log file.
endlocal
goto :EOF
:: The subroutine CheckLogSize must be called with two parameters: the log
:: file name without or with path as first parameter and the maximum file
:: size as second parameter.
:: Note: Windows command processor supports only 32 bit signed integers
:: which limits file size comparison to 2 GiB, i.e. 2147483647 bytes.
:: Therefore the second parameter should not be too large. It must be
:: made sure that the current file size of the log file is not exceeding
:: 2 GiB value on calling this subroutine. So better call this subroutine
:: once too often than once too rare.
:CheckLogSize
if not exist “%~1” goto :EOF
if %~z1 GTR %~2 move /Y “%~1” “%~dpn1_old%~x1” >nul
goto :EOF

The final visual basic file :-

Set WshShell = CreateObject(“WScript.Shell” )
WshShell.Run chr(34) & “C:\Scripts\MapNetworkDrives.bat” & Chr(34), 0
Set WshShell = Nothing

Place the file MapNetworkDrives.bat in C:\Scripts and the file MapNetworkDrives.vbs in %ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp

One thing of note. The drives often have a red cross on them. This is because the drives have powered down and windows can’t see them. Clicking on the drives brings them back online, so whilst it looks bad, it doesn’t stop the drives from still being mapped and working.

Trying to stop the red X

The first attempt was to run the following command from a DOS Prompt window, running as administrator.

net config server /autodisconnect:-1

If, for whatever the reason, you still want autodisconnect feature on but want a longer period to disconnect, you can use the same command as following:

net config server /autodisconnect:1440 #this will stop autodisconnect for 1440 minutes or 1 day

This didn’t work, so my new approach was to change the default icon for the mapped drives. How I achieved this can be found here

Leave a Reply

Your email address will not be published. Required fields are marked *