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