Script to remap current network drive?
Categories:
How to Remap a Network Drive Using Batch Files and VBScript

Learn various methods to remap network drives on Windows, including robust batch scripts and VBScript solutions for persistent and dynamic mapping.
Network drives are essential for accessing shared resources in many environments. However, they can sometimes become disconnected or require remapping due to network changes, server reconfigurations, or user profile issues. This article provides comprehensive solutions using batch files and VBScript to effectively remap network drives, ensuring reliable access to your shared folders. We'll cover basic remapping, handling existing connections, and creating persistent mappings.
Understanding Network Drive Mapping
Network drive mapping involves assigning a local drive letter to a shared network resource (e.g., \\server\share
). This makes it easier to access files and folders on the network as if they were stored locally. When a drive needs to be 'remapped,' it typically means disconnecting an existing mapping and then re-establishing it, often with updated parameters or to resolve connectivity issues. The NET USE
command is the primary tool in Windows for managing network drive connections.
flowchart TD A[Start Remap Process] --> B{Is Drive Already Mapped?} B -->|Yes| C[Disconnect Existing Drive] C --> D[Attempt New Mapping] B -->|No| D[Attempt New Mapping] D --> E{Mapping Successful?} E -->|Yes| F[End: Drive Remapped] E -->|No| G[End: Mapping Failed, Check Logs]
Workflow for remapping a network drive
Method 1: Basic Batch File Remapping
The simplest way to remap a network drive is to use a batch file that first disconnects the drive and then reconnects it. This method is straightforward and effective for most basic scenarios. You'll use the NET USE
command with the /DELETE
switch to remove the existing mapping and then without it to establish a new one. This approach is ideal for scripts run at login or as part of a system maintenance routine.
@echo off
REM Define variables
SET "DRIVE_LETTER=Z:"
SET "NETWORK_PATH=\\YourServer\YourShare"
SET "USERNAME=YourDomain\YourUser"
SET "PASSWORD=YourPassword"
REM Disconnect existing drive mapping (if any)
NET USE %DRIVE_LETTER% /DELETE /Y >NUL 2>&1
REM Map the network drive with credentials
NET USE %DRIVE_LETTER% %NETWORK_PATH% %PASSWORD% /USER:%USERNAME% /PERSISTENT:NO
IF %ERRORLEVEL% EQU 0 (
echo Successfully remapped %DRIVE_LETTER% to %NETWORK_PATH%
) ELSE (
echo Failed to remap %DRIVE_LETTER%. Error code: %ERRORLEVEL%
)
pause
Basic batch script to remap a network drive
WScript.Network
for production environments where credentials are required.Method 2: Robust Remapping with VBScript
VBScript offers more control and flexibility than batch files, especially when dealing with user interaction, error handling, and more complex logic. Using the WScript.Network
object, you can programmatically manage network drive connections. This method is particularly useful for creating persistent mappings or when you need to dynamically determine drive letters or network paths. VBScript can also hide the password from plain sight, making it slightly more secure than a batch file for credentialed mappings.
Dim objNetwork, strDriveLetter, strNetworkPath, strUserName, strPassword
' --- Configuration ---
strDriveLetter = "Y:"
strNetworkPath = "\\AnotherServer\AnotherShare"
strUserName = "YourDomain\YourUser"
strPassword = "YourPassword"
' ---------------------
Set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
' Disconnect existing drive mapping
objNetwork.RemoveNetworkDrive strDriveLetter, True, True
' Check for errors during disconnection
If Err.Number <> 0 Then
' Error 2147024894 (0x80070002) means drive not found, which is fine for deletion
If Err.Number <> -2147024894 Then
WScript.Echo "Error disconnecting drive " & strDriveLetter & ": " & Err.Description
Err.Clear
End If
End If
Err.Clear ' Clear any previous errors
' Map the network drive
objNetwork.MapNetworkDrive strDriveLetter, strNetworkPath, False, strUserName, strPassword
' Check for errors during mapping
If Err.Number <> 0 Then
WScript.Echo "Error mapping drive " & strDriveLetter & ": " & Err.Description
Else
WScript.Echo "Successfully remapped " & strDriveLetter & " to " & strNetworkPath
End If
Set objNetwork = Nothing
VBScript to remap a network drive with error handling
objNetwork.MapNetworkDrive
's third parameter (False
in the example) controls persistence. Set it to True
for persistent mapping (reconnects at login), or False
for non-persistent.Method 3: Remapping with Dynamic Drive Letter Assignment
In some scenarios, you might not want to hardcode a specific drive letter. You can modify the VBScript to find the next available drive letter and use that for mapping. This adds flexibility, especially in environments where drive letter assignments can vary or are limited. This example demonstrates how to iterate through potential drive letters and select the first available one.
Dim objNetwork, strNetworkPath, strUserName, strPassword
Dim strDriveLetter, i, bDriveFound
' --- Configuration ---
strNetworkPath = "\\YourServer\YourShare"
strUserName = "YourDomain\YourUser"
strPassword = "YourPassword"
' ---------------------
Set objNetwork = CreateObject("WScript.Network")
bDriveFound = False
' Iterate through possible drive letters (Z: down to E:)
For i = Asc("Z") To Asc("E") Step -1
strDriveLetter = Chr(i) & ":"
On Error Resume Next
objNetwork.RemoveNetworkDrive strDriveLetter, True, True ' Attempt to remove if already mapped
If Err.Number = 0 Or Err.Number = -2147024894 Then ' 0 = success, -2147024894 = drive not found
Err.Clear
' Try to map the drive
objNetwork.MapNetworkDrive strDriveLetter, strNetworkPath, False, strUserName, strPassword
If Err.Number = 0 Then
WScript.Echo "Successfully remapped " & strDriveLetter & " to " & strNetworkPath
bDriveFound = True
Exit For
Else
' Drive letter might be in use by something else, clear error and try next
Err.Clear
End If
End If
Next
If Not bDriveFound Then
WScript.Echo "Failed to find an available drive letter or remap " & strNetworkPath
End If
Set objNetwork = Nothing
VBScript to remap using the first available drive letter
On Error Resume Next
statement in VBScript is crucial for handling situations where a drive might not exist or is already in use without crashing the script. Always clear Err.Number
after handling an error.