12

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
  • 781
  • 1
  • 12
  • 25
  • 2
    Just out of curiosity, why do you think you need this? – Zoredache Oct 29 '13 at 18:47
  • 2
    I 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 Oct 29 '13 at 20:12
  • 1
    @GlenLittle Have you tried unchecking the "reconnect at logon" option when you map the drive? –  Oct 30 '13 at 02:09
  • No, I haven't. Most of the mappings are done with `net use`, but they are persistent. – Glen Little Oct 31 '13 at 23:03
  • 4
    Why not make a script to connect and disconnect the drives (including removing the mapping). Like [here](http://community.spiceworks.com/scripts/show/848-connect-or-disconnect-drives-for-vpn) You could even create a script which connect to the VPN automatically and a script to disconnect (including all the drive-mapping stuff) – Rik Nov 01 '13 at 22:18
  • 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 Nov 02 '13 at 03:04
  • 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 Nov 03 '13 at 10:20
  • @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 Nov 04 '13 at 16:46
  • 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 Nov 04 '13 at 18:01
  • Unfortunately, adding to the lmhosts file doesn't seem to help. – Glen Little Nov 05 '13 at 20:31
  • 4
    A `net use \\serverB\share2 /delete` should do the trick (it disconnects the drive but doesn't afflict the mapping) – Paolo Gibellini Nov 07 '13 at 07:28
  • @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 Nov 07 '13 at 09:36
  • 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 Nov 07 '13 at 10:22
  • 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 Nov 07 '13 at 22:01

3 Answers3

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

Jonathan Heady
  • 3,671
  • 22
  • 16
  • 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 Nov 13 '13 at 22:30
  • Powershell 4.0 was released a couple of weeks ago... http://technet.microsoft.com/en-us/library/hh847820.aspx – Glen Little Nov 13 '13 at 22:42
  • @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 Nov 13 '13 at 22:55
  • 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 Nov 15 '13 at 20:25
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
  • 13,159
  • 1
  • 34
  • 41
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
  • 521
  • 4
  • 7
  • 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 Dec 05 '13 at 04:44