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

32

14

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.

Stephen Jennings

Posted 2009-10-08T03:50:11.680

Reputation: 21 788

1What happens when you dir h:\\? – wfaulk – 2009-10-08T06:08:44.570

3In stead of typing "start.<enter>" you can just do win-E and save 5 keystrokes ;-) – fretje – 2009-10-08T13:21:25.210

1I "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 – 2013-05-03T11:41:10.843

Answers

12

Maybe try pushd \\server\share?

wfaulk

Posted 2009-10-08T03:50:11.680

Reputation: 5 692

1I 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 – 2015-03-20T04:42:43.370

This is the same as net use <temporary_letter> \\server\share && net use <temporary_letter> /del - but autogenerates <temporary_letter>. – ivan_pozdeev – 2016-12-15T02:18:26.670

2I'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. – Stephen Jennings – 2009-10-08T04:12:44.350

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 – 2009-10-08T04:40:34.717

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 – 2009-10-08T05:47:40.197

2I 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 – 2009-10-08T13:56:08.393

8

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!

Claus Melander

Posted 2009-10-08T03:50:11.680

Reputation: 81

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 – 2015-03-20T04:41:03.900

Btw, I don't find "taskkill" having case sensitivity, at least not on Win 7 x64 with latest patches. – prl77 – 2015-03-20T04:48:37.813

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 – 2016-07-11T11:54:04.377

Thx Luke for your comment. It really helped me – Pawel – 2017-02-27T22:50:43.717

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

Brian

Posted 2009-10-08T03:50:11.680

Reputation: 71

1I 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 – 2015-03-20T04:46:27.710

1It 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 – 2015-10-02T18:30:42.793

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... – Aaron – 2018-10-03T15:25:51.180

Given that we don't know the OP's level of expertise, it may be a good idea to expand on your answer. – Tog – 2013-11-05T14:44:22.260

net use works for me for Unavailable mapped drives in Win7. ty – Mat M – 2013-11-05T20:36:19.337

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.

wfaulk

Posted 2009-10-08T03:50:11.680

Reputation: 5 692

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 – 2016-12-15T01:59:50.967

2Command 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). – Stephen Jennings – 2009-10-09T03:49:47.120

1Weird. I swear it works for me. – wfaulk – 2009-10-09T14:43:43.180

it works for me as well! – Snark – 2009-10-09T14:47:07.153

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. – Stephen Jennings – 2009-10-10T06:20:16.737

1Oh, I bet that's it. I'm automatically authenticating using my domain credentials. – wfaulk – 2009-10-10T15:33:20.160

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.

Critical Skill

Posted 2009-10-08T03:50:11.680

Reputation: 183

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 – 2009-10-10T06:18:08.303

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

Warren

Posted 2009-10-08T03:50:11.680

Reputation: 21

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 – 2016-12-15T02:03:24.643

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.

StanZ

Posted 2009-10-08T03:50:11.680

Reputation: 11

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. – Elliot Cameron – 2012-09-27T20:59:24.943

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  

Charlie C

Posted 2009-10-08T03:50:11.680

Reputation: 21

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

thex

Posted 2009-10-08T03:50:11.680

Reputation: 201

0

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.

Zoran

Posted 2009-10-08T03:50:11.680

Reputation: 999

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 – 2009-10-08T05:41:03.627

Yes, I tested this just now and got "Local device name already in use." – Stephen Jennings – 2009-10-08T13:54:49.863

1It 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 – 2009-10-09T09:08:05.030

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.

MDMarra

Posted 2009-10-08T03:50:11.680

Reputation: 19 580

As an IT pro, I am opposed, both for myself and for my users. – ivan_pozdeev – 2016-12-15T02:06:15.443

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.

rkagerer

Posted 2009-10-08T03:50:11.680

Reputation: 248