How to create a batch file to map network drives after connected to VPN
Categories:
Automating Network Drive Mapping After VPN Connection

Learn how to create a robust batch file to automatically map network drives once your VPN connection is established, ensuring seamless access to remote resources.
Connecting to a Virtual Private Network (VPN) is a common practice for remote workers to securely access internal company resources. However, manually mapping network drives every time you connect can be tedious and error-prone. This article provides a comprehensive guide on creating a batch file that intelligently detects an active VPN connection and then proceeds to map the necessary network drives, streamlining your workflow.
Understanding the Challenge and Solution
The primary challenge lies in ensuring the batch file only attempts to map drives after the VPN connection is fully established. Attempting to map drives before the VPN is ready will result in errors. Our solution involves using a loop that continuously checks for the presence of a specific network adapter or IP address range associated with the VPN. Once detected, the script proceeds with the drive mapping commands.
flowchart TD A[Start Batch File] --> B{Is VPN Adapter Active?} B -- No --> C{Wait 5 Seconds} C --> B B -- Yes --> D[VPN Connected] D --> E[Map Network Drive 1] E --> F[Map Network Drive 2] F --> G[Map Network Drive N] G --> H[End Batch File]
Workflow for VPN-aware network drive mapping.
Identifying Your VPN Connection
Before writing the batch file, you need to identify a unique characteristic of your VPN connection. This could be the name of the VPN network adapter or an IP address range assigned by the VPN server. The ipconfig
command is invaluable for this. Look for an adapter that appears only when the VPN is active. Common VPN adapter names might include 'TAP-Windows Adapter V9', 'Cisco AnyConnect', or 'FortiClient SSL VPN'. Alternatively, you can check for an IP address within your corporate network's range.
@echo off
:: --- Configuration ---
SET "VPN_ADAPTER_NAME=Ethernet adapter VPN"
SET "DRIVE_LETTER_1=Z:"
SET "NETWORK_PATH_1=\\your-server\share1"
SET "DRIVE_LETTER_2=Y:"
SET "NETWORK_PATH_2=\\your-server\share2"
SET "WAIT_TIME=5"
:: --- Main Script ---
ECHO Checking for VPN connection...
:CHECK_VPN
ipconfig | findstr /i "%VPN_ADAPTER_NAME%" > nul
IF %ERRORLEVEL% NEQ 0 (
ECHO VPN adapter '%VPN_ADAPTER_NAME%' not found. Waiting %WAIT_TIME% seconds...
timeout /t %WAIT_TIME% /nobreak > nul
GOTO CHECK_VPN
)
ECHO VPN connection detected! Mapping network drives...
:: Map Drive 1
NET USE %DRIVE_LETTER_1% /DELETE /Y > nul
NET USE %DRIVE_LETTER_1% %NETWORK_PATH_1% /PERSISTENT:NO
IF %ERRORLEVEL% EQU 0 (
ECHO %DRIVE_LETTER_1% mapped successfully to %NETWORK_PATH_1%
) ELSE (
ECHO Failed to map %DRIVE_LETTER_1% to %NETWORK_PATH_1%
)
:: Map Drive 2
NET USE %DRIVE_LETTER_2% /DELETE /Y > nul
NET USE %DRIVE_LETTER_2% %NETWORK_PATH_2% /PERSISTENT:NO
IF %ERRORLEVEL% EQU 0 (
ECHO %DRIVE_LETTER_2% mapped successfully to %NETWORK_PATH_2%
) ELSE (
ECHO Failed to map %DRIVE_LETTER_2% to %NETWORK_PATH_2%
)
ECHO All specified drives processed.
PAUSE
ipconfig
. Look for an adapter that wasn't there before and note its description or name. You might need to adjust VPN_ADAPTER_NAME
to match exactly.Alternative: Checking for VPN IP Address
If checking the adapter name is unreliable or you prefer a more robust method, you can check for an IP address that is only available when the VPN is active. This often involves looking for an IP within a specific subnet used by your corporate network.
@echo off
:: --- Configuration ---
SET "VPN_IP_PREFIX=10.10.10."
SET "DRIVE_LETTER_1=Z:"
SET "NETWORK_PATH_1=\\your-server\share1"
SET "DRIVE_LETTER_2=Y:"
SET "NETWORK_PATH_2=\\your-server\share2"
SET "WAIT_TIME=5"
:: --- Main Script ---
ECHO Checking for VPN IP address...
:CHECK_VPN_IP
ipconfig | findstr /i "%VPN_IP_PREFIX%" > nul
IF %ERRORLEVEL% NEQ 0 (
ECHO VPN IP prefix '%VPN_IP_PREFIX%' not found. Waiting %WAIT_TIME% seconds...
timeout /t %WAIT_TIME% /nobreak > nul
GOTO CHECK_VPN_IP
)
ECHO VPN IP detected! Mapping network drives...
:: Map Drive 1
NET USE %DRIVE_LETTER_1% /DELETE /Y > nul
NET USE %DRIVE_LETTER_1% %NETWORK_PATH_1% /PERSISTENT:NO
IF %ERRORLEVEL% EQU 0 (
ECHO %DRIVE_LETTER_1% mapped successfully to %NETWORK_PATH_1%
) ELSE (
ECHO Failed to map %DRIVE_LETTER_1% to %NETWORK_PATH_1%
)
:: Map Drive 2
NET USE %DRIVE_LETTER_2% /DELETE /Y > nul
NET USE %DRIVE_LETTER_2% %NETWORK_PATH_2% /PERSISTENT:NO
IF %ERRORLEVEL% EQU 0 (
ECHO %DRIVE_LETTER_2% mapped successfully to %NETWORK_PATH_2%
) ELSE (
ECHO Failed to map %DRIVE_LETTER_2% to %NETWORK_PATH_2%
)
ECHO All specified drives processed.
PAUSE
VPN_IP_PREFIX
you choose is unique to your VPN connection and not present on your local network when the VPN is disconnected. A common mistake is using a prefix that might exist on both networks, leading to false positives.Running the Batch File Automatically
To make this solution truly seamless, you can configure the batch file to run automatically after your VPN connects. This can be achieved through various methods, such as using Windows Task Scheduler or integrating it directly into your VPN client's post-connection script if it supports such functionality.
1. Save the Batch File
Save the chosen batch file content (e.g., map_vpn_drives.bat
) to a convenient location on your computer, such as your Documents folder or a dedicated scripts folder.
2. Configure Task Scheduler
Open Windows Task Scheduler (search for 'Task Scheduler'). Create a new task. Set the trigger to 'On connection to VPN' or 'On event' if your VPN client logs a specific event upon connection. For the action, specify 'Start a program' and point it to your saved batch file.
3. Test Thoroughly
Disconnect from your VPN, then reconnect. Observe the batch file's execution. It should wait until the VPN is established and then map your drives. Check for any error messages in the command prompt window.