How to Disconnect a Mapped Network Drive in Batch Script
Managing network drive mappings is a common task in corporate environments and for users with home servers. While mapping a drive is a frequent operation, cleanly disconnecting it is just as important for security, resource management, and scripting logoff procedures. A script might need to unmap a drive to connect to a different server or to ensure a clean state before shutting down. The standard, built-in command for all network connection management is NET USE.
This guide will teach you how to use the NET USE command with its /DELETE switch to remove specific drive mappings, how to disconnect all mapped drives at once, and the best practices for making your scripts run silently and without errors.
The Core Command: NET USE
The NET USE command is the primary tool for managing network connections, including shared folders and printers. Its functionality covers creating (mapping), viewing, and deleting these connections.
To disconnect a mapped drive, you use the /DELETE switch (which can be shortened to /D).
The syntax is: NET USE [drive_letter:] /DELETE
Basic Example: Disconnecting a Specific Drive Letter
This is the most common use case: unmapping a single, known drive letter.
Let's disconnect a drive that was previously mapped to the letter Z:.
@ECHO OFF
ECHO Disconnecting the Z: network drive...
NET USE Z: /DELETE
In the output, when successful, the command prompt will confirm the action.
Disconnecting the Z: network drive...
Z: was deleted successfully.
Disconnecting All Mapped Drives at Once
In some scenarios, like a user logoff script, you may want to remove all network connections at once to ensure a clean slate for the next session. This can be done using a wildcard (*).
@ECHO OFF
ECHO Disconnecting all mapped network drives...
REM This will prompt for confirmation unless you add the /Y switch.
NET USE * /DELETE
This command is a powerful tool for resetting a user's network environment.
Key NET USE Parameters Explained
[drive_letter:]: The specific drive to disconnect (e.g.,Z:).*: A wildcard character that represents all currently mapped network connections./DELETEor/D: The switch that performs the disconnection./Y: (For Scripts) Automatically answers Yes to any confirmation prompts that may appear, especially when using the*wildcard. This is essential for non-interactive automation.
Common Pitfalls and How to Solve Them
Problem: "The network connection could not be found."
If you try to disconnect a drive letter that isn't currently mapped, the NET USE command will produce an error.
C:\> NET USE X: /DELETE
The network connection could not be found.
This error can clutter your script's output and is generally undesirable.
Solution: Check First with IF EXIST
The most robust way to handle this is to first check if the drive mapping exists before you try to delete it.
@ECHO OFF
SET "DRIVE_LETTER=X:"
IF EXIST "%DRIVE_LETTER%\" (
ECHO Drive %DRIVE_LETTER% exists. Disconnecting...
NET USE %DRIVE_LETTER% /DELETE
) ELSE (
ECHO Drive %DRIVE_LETTER% is not mapped. No action needed.
)
Problem: The Drive is "In Use"
If a program, a script, or even an open File Explorer window is actively using the mapped drive, Windows may prevent the disconnection.
For example:
The device is in use by an active process and cannot be disconnected.
The Solution: Close Handles or Use /Y
- Best Practice: The ideal solution is to ensure all programs that might be using the drive are closed before the script runs.
- Forceful Disconnect: Adding the
/Yswitch tellsNET USEto force the disconnection even if there are open connections. This is often effective.NET USE Z: /DELETE /Y
Problem: The Command Prompts for Confirmation
When using the wildcard (*), NET USE will stop and ask for confirmation, which breaks any automated script.
For example:
You have connections remaining. Continuing will cancel the connections.
Do you want to continue this operation? (Y/N) [N]:
Solution: Always Use /Y with the Wildcard
The /Y switch is mandatory for using the wildcard in a non-interactive script.
REM This is the correct, silent syntax for deleting all drives.
NET USE * /DELETE /Y
Practical Example: A Logoff Cleanup Script
This script is designed to be run automatically when a user logs off. It silently and forcefully disconnects all mapped network drives to ensure no connections are left open.
@ECHO OFF
SETLOCAL
ECHO --- Logoff Script: Disconnecting Network Resources ---
REM First, check if there are any connections at all by looking for the
REM "OK" status in the output of a plain 'net use' command.
NET USE | FIND "OK" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO No active network drives to disconnect.
GOTO :End
)
ECHO Disconnecting all mapped drives...
REM The '*' disconnects all. The '/Y' forces the action and suppresses prompts.
NET USE * /DELETE /Y
ECHO.
ECHO --- Cleanup complete ---
:End
ENDLOCAL
Conclusion
The NET USE command is the standard and most reliable tool for managing network drive mappings from a batch script.
For clean and robust disconnection scripts:
- Use
NET USE Z: /DELETEto remove a specific drive. - Use
NET USE * /DELETEto remove all drives. - Always add the
/Yswitch when using the wildcard (*) or when you need to force a disconnection, to make your script non-interactive. - For the most professional scripts, use
IF EXIST "Z:\"to check if a drive is mapped before attempting to disconnect it.