How to Make Network Drive Mappings Persistent in Batch Script
Mapping a network drive assigns a local drive letter (like Z:) to a network share (\\Server\Share), making it easy to access. However, a standard drive mapping created with NET USE is often temporary and will disappear after a system reboot. For critical shares that users need every time they log in, you must create a persistent mapping.
This guide will teach you how to use the /PERSISTENT switch of the built-in NET USE command to create drive mappings that automatically reconnect at every logon. You will learn how to make this the default behavior and how to check the status of your mappings.
The Core Command: NET USE
The NET USE command is the standard, built-in utility for managing all network connections, including drive mappings. It can be used to create, view, and delete these connections.
The basic syntax for mapping a drive is:
NET USE [drive_letter:] "\\ServerName\ShareName"
The Key to Persistence: The /PERSISTENT Switch
To make a mapping survive a reboot, you must explicitly tell the NET USE command to save the connection. This is done with the /PERSISTENT switch.
Syntax: NET USE [drive_letter:] "\\ServerName\ShareName" /PERSISTENT:YES
/PERSISTENT:YES: This tells the system to save this mapping and attempt to restore it automatically every time the user logs on./PERSISTENT:NO: This creates a temporary, one-time mapping that will be gone after a reboot.
Basic Example: Creating a Persistent Mapping
This script maps the Z: drive to a shared folder and ensures the mapping will be restored on the next logon.
@ECHO OFF
SET "SHARE_PATH=\\FileServer\SharedData"
SET "DRIVE_LETTER=Z:"
ECHO --- Creating a Persistent Network Drive Mapping ---
ECHO.
ECHO Mapping %DRIVE_LETTER% to %SHARE_PATH%...
NET USE %DRIVE_LETTER% "%SHARE_PATH%" /PERSISTENT:YES
IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The drive was mapped successfully and is persistent.
) ELSE (
ECHO [FAILURE] The command failed.
)
How to Check if a Mapping is Persistent
You can see the persistence setting for your mappings by running NET USE with no arguments.
Command: NET USE
The output will list all current connections. Pay attention to the first line, "New connections will be remembered" or "New connections will not be remembered."
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK Z: \\FileServer\SharedData Microsoft Windows Network
The command completed successfully.
"New connections will be remembered" indicates that /PERSISTENT:YES is the current default.
How to Change the Default Behavior
Constantly typing /PERSISTENT:YES can be tedious. You can change the default setting for all future NET USE commands in your session.
Command to Set the Default: NET USE /PERSISTENT:YES
After you run this command once, any subsequent NET USE Z: \\Server\Share command will automatically be persistent without you needing to add the switch. This setting itself is persistent across reboots.
To make future connections temporary by default, you would run: NET USE /PERSISTENT:NO
Common Pitfalls and How to Solve Them
Problem: The Mapping Fails on the Next Logon (Credentials)
This is the most common issue. The drive mapping is saved, but on the next logon, Windows shows a "Could not reconnect all network drives" error. This almost always happens when the user's password for the network share is different from their Windows login password.
Solution: When you first map the drive, provide the username and password and tell NET USE to save them.
REM The /USER switch specifies the username.
REM The password is provided after the share path.
REM The /SAVECRED switch tells Windows to save these credentials.
NET USE Z: "\\FileServer\Share" "MySecretPassword" /USER:"FileServer\ShareUser" /SAVECRED /PERSISTENT:YES
The /SAVECRED switch stores the credentials in the Windows Credential Manager, allowing Windows to automatically authenticate and reconnect the drive at the next logon.
Problem: The Drive Letter is Already in Use
If you try to map to a letter that is already being used (by a local drive, a USB drive, or another mapping), the command will fail.
Solution: A robust script should check if the drive letter is free before attempting to map it. You can do this with IF NOT EXIST.
IF NOT EXIST "Z:\" (
NET USE Z: "\\FileServer\Share" /PERSISTENT:YES
) ELSE (
ECHO The Z: drive is already in use.
)
Practical Example: A User Logon Script
This script is designed to be run automatically when a user logs in. It ensures that three critical company network shares are always mapped.
@ECHO OFF
REM This is a user logon script.
ECHO --- Verifying company network drives ---
REM First, ensure the default is persistent.
NET USE /PERSISTENT:YES > NUL
REM --- Map the Public share to P: ---
IF NOT EXIST "P:\" (
NET USE P: "\\CompanyServer\Public"
)
REM --- Map the Finance share to F: ---
IF NOT EXIST "F:\" (
NET USE F: "\\CompanyServer\Finance"
)
REM --- Map the User's Home Directory to H: ---
IF NOT EXIST "H:\" (
NET USE H: "\\UserDataServer\Users\%USERNAME%"
)
This script is idempotent, meaning it can be run multiple times safely. It will only map the drives that are not already connected.
Conclusion
The /PERSISTENT:YES switch is the key to creating network drive mappings that are reliable and survive reboots.
Key takeaways for success:
- Use
NET USE Z: "\\Server\Share" /PERSISTENT:YESto create a single persistent mapping. - Run
NET USE /PERSISTENT:YESonce to make all future mappings in your session persistent by default. - If you need to provide credentials, use the
/SAVECREDswitch to store them for future logons. - For robust scripts, check if a drive letter is free with
IF NOT EXIST "Z:\"before you try to map it.