38

On a daily basis I find myself in the Windows command prompt needing to access a network drive that is mapped but disconnected. I have yet to find a command that will reconnect this drive without unmapping and remapping (which leads to a password guessing game, since I don't own these computers). I would also like to be able to script this so every night the drive is reconnected if it has become disconnected somehow.

The fastest solution I currently have is to:

  1. Type "start." to open explorer,
  2. Alt-D to focus the address bar,
  3. type the drive letter I want and press enter, and wait for it to display the drive contents,
  4. then finally, close explorer and go back to the command prompt.

I know it's a minor inconvenience, but I'm often doing this through a slow VNC or PCAnywhere connection where doing anything through GUI is awful, so I'm just wondering if there's a better solution.

3
  • 1
    What happens when you `dir h:\`?
    – wfaulk
    Oct 8, 2009 at 6:08
  • 4
    In stead of typing "start.<enter>" you can just do win-E and save 5 keystrokes ;-)
    – fretje
    Oct 8, 2009 at 13:21
  • 1
    I "solved" it by just using the \\server\share path instead of the mapped drive. You can just copy files if you need to. (That was my problem)
    – Peterdk
    May 3, 2013 at 11:41

12 Answers 12

15

Maybe try pushd \\server\share?

6
  • 3
    I'm not sure this is what I need. Pushd looks like it creates temporary drive letters; I already have a drive letter mapped, but the share is disconnected (it has a red X in explorer and is labeled "Disconnected Network Drive"). Maybe I am mistaken. Oct 8, 2009 at 4:12
  • 1
    Yeah, I was thinking that accessing it that way might poke the drive into reconnecting. You can use popd to remove the drive letter.
    – wfaulk
    Oct 8, 2009 at 4:40
  • I will try it next time I encounter this. I suspect it will either fail saying the device is in use, or connect the wrong drive letter to the share. Oct 8, 2009 at 5:47
  • 2
    I tried this and it actually did work. I first ran pushd to the share then popd to unmap the extra drive letter, and I found the mapped drive I wanted was connected again. This is definitely an upvote, and if I don't get a shorter answer in a couple days this will be accepted. Oct 8, 2009 at 13:56
  • 2
    I don't have downvoting capability yet, but this answer did nothing for me. See several posts below to see which did. I also left a comment there.
    – prl77
    Mar 20, 2015 at 4:42
10

I have to start Windows 7 before I can run the VPN program which connects me to the company network drives. Therefore my network drives don't automatically reconnect, only opening them in Windows Explorer reconnects them.

I have made a small batch file to start the VPN, reconnect the network drives, and start some applications I always uses.

In my Batch file I have the following:

REM Connect VPN here...

REM Opens an Explorer window looking at T: forcing a reconnect
Start /min explorer t:\

timeout 3 /nobreak

REM Kill all Explorer windows beginning with "T_drive" in the title
Taskkill /fi "windowtitle eq T_drive*"

REM Finish starting up here...

exit

The Taskkill /fi "windowtitle eq" command is case sensitive!

3
  • Thanks man, this was the only answer on this whole thread that helped me. Of course I upvoted it, but I don't have downvote capability yet for those posts above you.
    – prl77
    Mar 20, 2015 at 4:41
  • Btw, I don't find "taskkill" having case sensitivity, at least not on Win 7 x64 with latest patches.
    – prl77
    Mar 20, 2015 at 4:48
  • 1
    Note: If the taskkill parameters above don't work for you, try using TASKLIST /FI "IMAGENAME eq explorer.exe" /V to see what the correct window title is. My filter string for TASKKILL ended up being "windowtitle eq c$*"
    – Luke
    Jul 11, 2016 at 11:54
7

create a batch file (refreshletters.cmd) with these commands in it

(these will only work inside a batch file)

Tested on on Win7 and XP to refresh 'Disconnected' and 'Unavailable' driver letters in a console window (command line).

@echo off
net use |FIND ":" > %temp%\used.txt
FOR /F " tokens=1,2,3 delims= " %%i in (%temp%\used.txt) do (

  if %%i EQU Unavailable  (
    net use %%j %%k
    echo Activated %%j
   ) ELSE (
     if %%i EQU Disconnected (
       pushd .
       cd /d %%j
       dir . %>nul
       if NOT exist %%j\. (
          net use %%j /del /y
          net use %%j %%k
          echo Remapped %%j
          ) else (
             echo Fixed-up %%j
            )
       popd
       ) ELSE (
          echo Checked %%j
          )
     )
 )
5
  • Given that we don't know the OP's level of expertise, it may be a good idea to expand on your answer.
    – Tog
    Nov 5, 2013 at 14:44
  • net use works for me for Unavailable mapped drives in Win7. ty
    – Mat M
    Nov 5, 2013 at 20:36
  • 1
    I don't have downvoting capability yet, but this answer did nothing for me. See the post below by Claus Melander which did. I also left a comment there.
    – prl77
    Mar 20, 2015 at 4:46
  • 1
    It works (tested with win7 x64), but it depends on the language dependent output of net use, as it scans for Unavailable. But for a german win7 you get Nicht verfgb, you need to modify the solution to your language
    – jeb
    Oct 2, 2015 at 18:30
  • This worked for me, except for one share that had a space in the network path... Anyway, I just ended up doing some hard-coded "net use X: \\share\path" lines in a batch-file and I will keep it up to date if I ever change the drive mappings. This is the best solution for me because I really don't want to have to pop open an Explorer window to get the drive to reconnect... Oct 3, 2018 at 15:25
6

This is probably a stupid question, but assuming your "disconnected" drive is H:, have you tried just changing directly to the drive?

It works for me under XPSP3:

C:\>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
Disconnected H:        \\xxxxxx\wfaulk           Microsoft Windows Network
OK                     \\xxxxxxx\business        Microsoft Windows Network
The command completed successfully.


C:\>h:

H:\>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           H:        \\xxxxxx\wfaulk           Microsoft Windows Network
OK                     \\xxxxxxx\business        Microsoft Windows Network
The command completed successfully.
6
  • 3
    Command prompt does not let you switch to a network drive which is currently disconnected. At least not on the hundreds of computers I'm using this on (WinNT, Win2k, WinXP, WinXP Embedded). Oct 9, 2009 at 3:49
  • 1
    Weird. I swear it works for me.
    – wfaulk
    Oct 9, 2009 at 14:43
  • it works for me as well!
    – Snark
    Oct 9, 2009 at 14:47
  • I admit I have seen this work occasionally, but most of the time it does not. I don't know what determines one way or another, possibly whether or not there are saved credentials. Oct 10, 2009 at 6:20
  • 1
    Oh, I bet that's it. I'm automatically authenticating using my domain credentials.
    – wfaulk
    Oct 10, 2009 at 15:33
4

I know you said "net use" didnt work for you - but here is what you could try.

Run a batch file with net use /DELETE option to remove the existing/persistent connections And then you could use net use with the /SAVECRED option to re-use the passowd credentials used int eh last succesful logon. I am not sure if this will address your problem but its worth looking into.

1
  • This isn't quite what I need, but this is definitely a useful thing to know. This solves another problem I've had. Oct 10, 2009 at 6:18
2

I have the same problem with multiple connections, and it's intermittent. When I reboot the computer, sometimes the connection is "good" and sometimes not. I don't have the password problem since I'm on a company LAN, but still I have the other aspect of the problem.

I've created a batch file called ex.bat, and stored it (along with many other things) in a \util directory that I put in my path. I can type "ex h:" and it will run Windows Explorer and set it to go directly to the h: drive. Here is the batch file:

start explorer /e,/select,%1

This works, with a lot fewer keystrokes. Or if it doesn't work at first, hit the Enter key to open the h: root folder and display the contents. Or, type "ex h:\sub" where the subfolder "sub" exists.

This problem seems to have gotten a lot worse recently, perhaps due to one of the Windows XP security patches.

Another thing to try is on the server side. On Windows servers, modifying a Registry key is supposed to keep the server from disconnecting its connections on a timeout. I do this on pretty much all our servers. The file is:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters]
"autodisconnect"=dword:ffffffff
1
  • From a process run as another user, start explorer would only work if both users have the "Run folder windows in a separate process" setting. Dec 15, 2016 at 2:03
1

Just type:

NET USE H: \\server\share

(where H: is your drive letter of course)

Type 'NET /? USE' to see all options this command gives you.

4
  • This does not work. The drive letter is already mapped, it's just disconnected (usually the other computer was rebooted). When you run this, it complains that the device is already in use. Oct 8, 2009 at 5:41
  • Yes, I tested this just now and got "Local device name already in use." Oct 8, 2009 at 13:54
  • 1
    It works for me... but I'm on Windows 7, and actually the drive reconnects by itself... Try NET USE H: simple (without the path), that should bring the connection back up. I forget how it used to be in XP.
    – Zoran
    Oct 9, 2009 at 9:08
  • On Windows 10 (13 years later) this is the only way to ensure that script will work. It reconnects drive (there is no warnig that drive is in use, if letter and share match).
    – Marek
    Jun 8, 2022 at 10:55
1

I had a similar problem and discovered I could force a reconnection with a simple "dir x:" from the command line (where X: was the mapped but disconnected drive.) This was from a Server 2003 system to mapped drives on XP and Win7 computers.

I created a batch file, wake_network_drives.bat, with these two lines:

    dir x:
    dir z:

and set a scheduled task to run it nightly before the backup program that was failing because the drives were occasionally showing as disconnected.

1
  • This works. Even if the dir command gives you an error, it tends to wake up the disconnected network drive. This is the best answer of them all. Sep 27, 2012 at 20:59
0

If you aren't opposed to keeping these passwords in plain text in a batch you can script a delete and remap including credentials.

net use H: /delete
net use H: \\server\share /user:domain\user password /persistent:yes

I know it doesn't meet your requirement of not deleting the share, but it resolves the root of the problem of having to remember passwords.

1
  • As an IT pro, I am opposed, both for myself and for my users. Dec 15, 2016 at 2:06
0

TL;DR summary: use net use and if exist

I ran into a related problem while creating a nightly backup script. It had to check if a computer was turned on (using ping), send a magic packet to turn it on if needed (using MC-WOL), wait for Windows to boot, then establish a connection to an SMB share and wait for it to be present.

While other answers here are great, I didn't want mapped drive letters popping in / out of existence, or to disconnect anything the user might be working with, nor to have Explorer windows flashing around. i.e. A goal was zero user distraction.

Here's a chunk of code that may be helpful to you:

set TARGET_IP=192.168.1.3
set TARGET_PATH=\\%TARGET_IP%\d$\backups
set ATTEMPTS=0

:LOOP

REM Give up eventually
if %ATTEMPTS% GEQ 200 goto NOJOY

REM Wait for IP connectivity to host, and/or burn up a couple seconds before retry
ping -n 1 -w 2000 %TARGET_IP% | find "TTL" > nul
if errorlevel 1 set /a ATTEMPTS+=1 & goto LOOP

REM See if we're already connected, and try to give it a kick
if exist %TARGET_PATH% goto CONNECTED

REM Try to reconnect
net use %TARGET_PATH% > nul
if errorlevel 1 set /a ATTEMPTS+=1 & goto LOOP

:CONNECTED
echo Connected
exit /b

:NOJOY
echo Couldn't connect

If you prefer, you can replace the IP address above with a host name (e.g. server1).

net use is needed in case no existing connection is present. I ran into some corner cases where it alone seemed insufficient to "wake up" the share (e.g. if there was a prior connection existing in a disconnected state). I found the if exists directive has a similar effect as the alternatives mentioned in other answers (like pushd, dir, Explorer, etc), but it seemed a lighter-weight approach.

0

The answer from StanZ worked for me, but I had to be able to programatically determine any mapped drives without a status of OK, as users in our domain get different drive mappings. I coded here to run as a stand alone version, which executes net use before and after the actual code, so that you can see the results, but you only need to copy the one FOR statement (three lines) into your code to get the desired results.

@ECHO OFF
::  
:: revision of answer from StanZ on superuser.com at the following url  
:: https://superuser.com/questions/52595/can-a-mapped-network-drive-be-reconnected-from-the-command-line/367910#367910  
:: https://superuser.com/a/367910  
:: which resulted in my  
::  
:: DriveStatusOK.bat  
:: iCharlieC  
::  
SETLOCAL ENABLEEXTENSIONS  
::  
:: you need only copy the one FOR statement (three lines)  
::   to use in your own code  
::   to force status of mapped network drives to OK  
::  
:: "tokens=1-2 delims=: " is used in the for statement  
:: to output only the drive letter from the output of...  
:: net use  
:: is piped to the first findstr which outputs only lines with drive letters  
::   then piped to another findstr to eliminate drives with a status of OK  
::   then piped to another findstr to get lines that start with a status  
::     -in other words, those lines that the first character is a letter-  
::     -this due to the fact that in our domain we have a drive  
::      that is mapped to a letter, but never has a status.  
::      I do not know the reason for this,  
::      but adding that last findstr prevents me from getting an error  
::      from trying to execute a dir on a non-existant drive letter.-  
::   then dir is executed on each drive letter (after appending ":\" to the letter)  
::      and the output redirected to NUL  
::  
:: run net use before as a visual here  
net use  
::  
FOR /F "tokens=1-2 delims=: " %%i in (  
    'net use ^| findstr ":" ^| findstr /i /v "^OK" ^| findstr /i "^[A-Z]"'  
    ) do dir "%%j:" > NUL  
::  
:: run net use after "waking" drive(s)  
net use  
::  
ENDLOCAL  
0

Maybe an old question but still relevant in 2017 and Windows 10 because network shares are often not reconnected or attached corretly by windows for different reasons. Based on this problem and the absence of a real praticable and full working solution I created a Github project which contains a Scheduler Task and a VBS which solves the issue by simply trying to reconnect drives after resume or logon if not connected or online.

The script is full configureable and can be set up depending on the needs of the actual user.

Path to the Github project

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .