Is there a way to "disconnect" a mapped network drive, but NOT remove the mapping?

12

3

For example, when I type net use I might see something like:

Status        Local       Remote
OK            H:          \\server\home folders\me
Unavailable   I:          \\serverA\share1
Disconnected  J:          \\serverB\share2
Reconnecting  K:          \\serverC\share3

When this happens, Windows Explorer has 10 - 20 second delays when doing something as simple as creating a new folder on my C: drive. After a while, the K: drive will change to say "Disconnected".

What I want to do is somehow force the K: drive to be Disconnected or Unavailable, but without losing the authenticated mapping.

Glen Little

Posted 2013-10-29T18:42:29.980

Reputation: 791

2Just out of curiosity, why do you think you need this? – Zoredache – 2013-10-29T18:47:05.313

2I connect to a VPN, use the K: drive, then disconnect the VPN. After that, my Explorer is often slow, until it finally marks K: as disconnected. This is a simplification, as I have more than 3 drive mappings needed by various scripts, and may connect/disconnect from the VPN multiple times a day. – Glen Little – 2013-10-29T20:12:07.477

1@GlenLittle Have you tried unchecking the "reconnect at logon" option when you map the drive? – Moses – 2013-10-30T02:09:49.817

No, I haven't. Most of the mappings are done with net use, but they are persistent. – Glen Little – 2013-10-31T23:03:42.523

4

Why not make a script to connect and disconnect the drives (including removing the mapping). Like here You could even create a script which connect to the VPN automatically and a script to disconnect (including all the drive-mapping stuff)

– Rik – 2013-11-01T22:18:52.800

Thanks Rik. Interesting script (sort of). But I wouldn't want to store all the passwords in the file. I may need to do something similar though... – Glen Little – 2013-11-02T03:04:03.610

Try disabling the network adapter associated with the VPN connection (once you're done with it) and check if it helps with the Explorer delay. – Kedar – 2013-11-03T10:20:51.817

@kedar I only have one adapter. The VPN "adapter" cannot be disabled, only disconnected. However, I did try disabling/enabling the main adapter and it did not seem to help. – Glen Little – 2013-11-04T16:46:13.043

Does adding the hostnames of servera, serverb etc with their ip jumbers in lmhosts help? That way the hosts don't need to be searched on the network and the ip times-out directly. (Not the hosts but lmhosts) – Rik – 2013-11-04T18:01:39.800

Unfortunately, adding to the lmhosts file doesn't seem to help. – Glen Little – 2013-11-05T20:31:02.180

4A net use \\serverB\share2 /delete should do the trick (it disconnects the drive but doesn't afflict the mapping) – Paolo Gibellini – 2013-11-07T07:28:22.087

@PaoloGibellini have you tried this yourself? I get a The network connection could not be found. error with this (using my own drive-specs of course). If you mean net use J: /delete that really deletes the mapping. – Rik – 2013-11-07T09:36:12.837

1@Rik Thank you, I hadn't tried it and yes, if a unit is mapped to a drive letter you must use the letter: net use J: /delete is the correct way. You should use the full path when you are connected to a network share without mapping it. Anyhow, at the next restart your mapping will be restored (either if created through a batch file using /persistent:yes or using the windows user interface). – Paolo Gibellini – 2013-11-07T10:22:38.587

You can not map it in the first place. Access the share without a drive letter mapping with net use \server\share\vol /user:domain\username password then just access the folder \server\share\vol – Back2Basics – 2013-11-07T22:01:43.733

Answers

1

Microsoft seems to be trying to move administrative tasks such as this to PowerShell.

In your specific case, the cmdlet to use is Remove-PSDrive. Remove-PSDrive will do exactly what you are asking for: disconnect a mapped network drive. The syntax for your scenario would be:

Remove-PSDrive -Name K

To get to a powershell command line where you can run that cmdlet, open the start menu, and in the search box just type powershell. If you don't have the search box, then from the start menu select Run and in the run box type powershell and press enter.

For full details on how to use this cmdlet including all of it's options see Microsoft's technet article:
http://technet.microsoft.com/en-us/library/hh849760.aspx

Matrix Mole

Posted 2013-10-29T18:42:29.980

Reputation: 3 303

Sounded very promising, but doesn't work for me. If I do net use y: ...., then start PowerShell and do get-psdrive, I see Y listed. Doing remove-psdrive -Name Y removes it. But then a net use still shows it, and I can still use the Y: drive. (That was using PS v2. Apparently works with v4.) – Glen Little – 2013-11-13T22:30:40.963

Powershell 4.0 was released a couple of weeks ago... http://technet.microsoft.com/en-us/library/hh847820.aspx

– Glen Little – 2013-11-13T22:42:34.690

@GlenLittle But does this remove the drive-letter or disconnect it while keeping the letter? If it removes it, isn't this the same as net use Y: /delete? – Rik – 2013-11-13T22:55:53.897

Finally able to try it. Unfortunately, this seems to mimic Net use /delete as @Rik guessed. I don't see a difference, as far as network drives are concerned. – Glen Little – 2013-11-15T20:25:07.077

1

Ok. I might have a solution. I tried looking into the /savecred parameter of net use but couldn't get it to work properly so here is another approach:

Your trouble was the delay with Explorer after the disconnect with the VPN.
So why not hide the drives from Explorer after the disconnect so it won't try to access them.

There is a registry key to hide drives from Explorer:

HKEY_xxx\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives

and HKEY_xxx can both be HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER.

Below this post there is a summary as to what value NoDrives should hold.
(add the numbers for the drives).


I've created a small batchfile where you don't have to do the calculations yourself.
It is a 'toggle'-batchfile.

Run it once to connect to your "My VPN".
Run it again to disconnect and hide the drives I, J and K.

You can easily split this in two batchfiles but you get the point... You could also simplify this batchfile if you set the number of NoDrives yourself instead of letting the batch do the calculation.

This needs to be run as administrator. (it is possible to automatically elevate but that's very advanced)

If you grant yourself permission on the key NoDrives you don't have to run this as administrator. Just open up regedit.exe, browse to this key and click "Edit > Permissions" and grant yourself "Full Control" over this key.

@echo off
:: ------------------------
set MyVPN="My VPN"
:: ------------------------
rasdial | findstr %MyVPN% 1>nul
If %ERRORLEVEL% == 0 goto disconnect

:connect
echo ============================================================
echo connecting to %MyVPN%
echo ============================================================
rasdial %MyVPN%
:: This is easy. We just set 0 in NoDrives
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoDrives /T REG_DWORD /d 0 /f

goto :end

:disconnect
echo ============================================================
echo disconnecting from %MyVPN%
echo ============================================================
rasdial %MyVPN% /disconnect

:: ------------------------------------------------------------------
:: This is harder. We need to determine what value NoDrives should be
:: ------------------------------------------------------------------
Setlocal EnableDelayedExpansion

:: ------------------------
:: Set here your VPN drives
:: ------------------------
set drives='I J K'

:: ------------------------
:: Calculation to determine the value
:: ------------------------
set drive_value=1
set NoDrives=0
for %%c in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  echo.%drives% | findstr "%%c" 1>nul
  if !ERRORLEVEL! == 0 set /a NoDrives+=drive_value
  set /a drive_value="drive_value<<1"
)
echo Seting NoDrives to %NoDrives%
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoDrives /T REG_DWORD /d %NoDrives% /f
endlocal

:end
pause

Note:
Windows Explorer needs to be closed and opened again before the drives are hidden (or visible again).


Settings for NoDrives:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives

     Decimal       Hex
A:         1         1
B:         2         2
C:         4         4
D:         8         8
E:        16        10
F:        32        20
G:        64        40
H:       128        80
I:       256       100
J:       512       200
K:      1024       400
L:      2048       800
M:      4096      1000
N:      8192      2000
O:     16384      4000
P:     32768      8000
Q:     65536     10000
R:    131072     20000
S:    262144     40000
T:    524288     80000
U:   1048576    100000
V:   2097152    200000
W:   4194304    400000
X:   8388608    800000
Y:  16777216   1000000 
Z:  33554432   2000000 

Rik

Posted 2013-10-29T18:42:29.980

Reputation: 11 800

Looks interesting... I'll have to try this. Love the use of bitwise math in the batch file! – Glen Little – 2013-12-09T21:43:53.970

0

I don't know how to do this, but would make a script to add the mapped drive to make life easy.

  1. Create a file with the extension .bat (Ex. map_drives.bat)
  2. Inside the file add a line like this net use J: \\server\folder
  3. Double click on that file and it will map your drive

mvndaai

Posted 2013-10-29T18:42:29.980

Reputation: 326

Thanks for the answer. I would do this, but some of the drives need different id/pw authentications, and I don't want to put those into a script. – Glen Little – 2013-12-05T04:44:13.127