Remapping Network Shares at Login

I map a couple of drives from my NAS on my machine and for some reason they stopped reattaching correctly at login. I searched high and low for an answer but that’s the sort of question that generates millions of useless woo woo solutions. In the end I write a little command script to remap them for me at start up.

First create the file that will hold the commands. Press Win+r and enter “shell:startup” this will open Windows Explorer at your start up folder (typically: C:\Users\[account]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup). Create a file called “reconnect.cmd”.

Open “reconnect.cmd” in your favourite text editor (such as Notepad++) and enter the following script:

@echo off

set /a counter=0
set /a limit=10

:MediaStart
if /I "%counter%" GEQ "%limit%" goto GiveUp
set /a counter=%counter%+1

timeout /t 5 /nobreak >NUL
if exist M:\NUL goto MediaEnd
net use M: \\NASSERVERNAME\media /PERSISTENT:YES
if ERRORLEVEL 1 goto MediaStart
:MediaEnd

:GiveUp

Change “\\NASSERVERNAME\media” to point at the share you want to make and the “M:” to the drive you want to map it to.

As far as I could tell from my research the reason the drives weren’t mapping automatically was because the networking system wasn’t ready when the system tried to map the drives leading to a failure. Scripts in the start up folder are run right at the end of the start up so networking is ready then. Just in case it isn’t I try to map the drives up to ten times before giving up, I’ve never seen it need more than a single go though.