Can a mapped network drive be reconnected from the command line?

87,326

Solution 1

Maybe try pushd \\server\share?

Solution 2

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!

Solution 3

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
          )
     )
 )

Solution 4

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.

Solution 5

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.

Share:
87,326

Related videos on Youtube

Stephen Jennings
Author by

Stephen Jennings

Updated on September 17, 2022

Comments

  • Stephen Jennings
    Stephen Jennings over 1 year

    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.

    • wfaulk
      wfaulk over 14 years
      What happens when you `dir h:\`?
    • David
      David about 11 years
      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)
  • Stephen Jennings
    Stephen Jennings over 14 years
    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.
  • wfaulk
    wfaulk over 14 years
    Yeah, I was thinking that accessing it that way might poke the drive into reconnecting. You can use popd to remove the drive letter.
  • Stephen Jennings
    Stephen Jennings over 14 years
    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.
  • Stephen Jennings
    Stephen Jennings over 14 years
    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.
  • Stephen Jennings
    Stephen Jennings over 14 years
    Yes, I tested this just now and got "Local device name already in use."
  • Stephen Jennings
    Stephen Jennings over 14 years
    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.
  • Stephen Jennings
    Stephen Jennings over 14 years
    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).
  • wfaulk
    wfaulk over 14 years
    Weird. I swear it works for me.
  • Snark
    Snark over 14 years
    it works for me as well!
  • Stephen Jennings
    Stephen Jennings over 14 years
    This isn't quite what I need, but this is definitely a useful thing to know. This solves another problem I've had.
  • Stephen Jennings
    Stephen Jennings over 14 years
    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.
  • wfaulk
    wfaulk over 14 years
    Oh, I bet that's it. I'm automatically authenticating using my domain credentials.
  • Elliot Cameron
    Elliot Cameron over 11 years
    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.
  • Tog
    Tog over 10 years
    Given that we don't know the OP's level of expertise, it may be a good idea to expand on your answer.
  • Mat M
    Mat M over 10 years
    net use works for me for Unavailable mapped drives in Win7. ty
  • Lari Hazanovich
    Lari Hazanovich about 9 years
    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.
  • Lari Hazanovich
    Lari Hazanovich about 9 years
    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.
  • Lari Hazanovich
    Lari Hazanovich about 9 years
    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.
  • Lari Hazanovich
    Lari Hazanovich about 9 years
    Btw, I don't find "taskkill" having case sensitivity, at least not on Win 7 x64 with latest patches.
  • jeb
    jeb over 8 years
    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
  • Luke
    Luke almost 8 years
    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$*"
  • ivan_pozdeev
    ivan_pozdeev over 7 years
    I tested in Win7 that saved credentials have no effect. All they affect is not needing to include /user into the normal connect command.
  • ivan_pozdeev
    ivan_pozdeev over 7 years
    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.
  • ivan_pozdeev
    ivan_pozdeev over 7 years
    As an IT pro, I am opposed, both for myself and for my users.
  • ivan_pozdeev
    ivan_pozdeev over 7 years
    This is the same as net use <temporary_letter> \\server\share && net use <temporary_letter> /del - but autogenerates <temporary_letter>.
  • Aaron
    Aaron over 5 years
    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...
  • Admin
    Admin almost 2 years
    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).