Check and delete obsolete mapped network drives on workstations (batch)

Following the article of  Share Migration, it can be interesting to know which computers have those network drives mapped.
We can get this information without using powershell, just adding a simple batch to users' logonscript will be enough.

Persistent network drives are stored in registry in HKCU\Network.


We go over the subkeys with a for loop and ask each of them if the value of RemotePath points to the old server. We can even delete them directly and leave a log of found paths.

@echo off
for /f "tokens=1" %%a in ('reg query "HKEY_CURRENT_USER\Network" ') DO (
 echo Checking path %%a
 reg query %%a /v "RemotePath"| findstr /I /C:"svbpnaclfs01" /C:"svbpnaclfs02" && (echo Obsolete path found en %%a & reg delete %%a /f & echo %date% %time% %computername% %username% %%a %errorlevel% >>\\mymachine\logs$\obsoletenetworkdrives.log) || echo OK
)

Comments