Connect Shared Folders Using a Drives.bat File
You can map a network drive with a Drives.bat file by putting a net use command in it and running the file on Windows. The command assigns a drive letter, such as I:, to an existing shared folder. Start with a simple mapping, then add separate credentials, additional shares, or sign-in automation if you need them.
What a Drives.bat File Does
Drives.bat is an ordinary Windows batch file, not a special Windows system file. When you run it, Command Prompt executes its commands in order.
A network-drive mapping connects a drive letter to a Universal Naming Convention, or UNC, path:
I:is the drive letter Windows will display.\\fileserver\shareidentifies the server and its shared folder.- The server can be a Windows file server or a compatible NAS device.
The batch file runs on the Windows computer accessing the share, not on the NAS.
Mapping a drive does not create a shared folder or grant permission to use it.
The shared folder must already exist, the server must be reachable, and your account must have access. A script cannot bypass share permissions or file-system permissions.
How to Map a Network Drive With Drives.bat
Choose the authentication approach before creating the file.
| Situation | Approach |
|---|---|
| Your Windows account already opens the share | Map it without specifying another account. |
| The server requires a different account | Specify that account and request its password interactively. |
| The mapping must run unattended at sign-in | Establish working password-free authentication before deploying the login script. |
Step 1: Check the Shared Folder and Drive Letter
- Connect to the network that hosts the server. If the share requires your organization’s VPN, connect it first.
- Enter the share’s UNC path, such as
\\fileserver\share, in File Explorer’s address bar. - Confirm that you can open the folder. If Windows refuses access or cannot find it, resolve that problem before writing the script.
- Choose an unused drive letter. The following example uses
I:.
To inspect existing network connections, run this in Command Prompt:
net use
Also check File Explorer for local disks and removable drives. A letter occupied by a local device is not available for your network mapping.
Step 2: Create the Batch File
- Open a plain-text editor, such as Notepad.
- Paste this script:
@echo off
net use I: "\\fileserver\share"
pause
- Replace
\\fileserver\sharewith the actual UNC path. - Change
I:if you chose another available letter.
The mapping command uses the credentials Windows can supply for the connection. The quotation marks keep a path containing spaces together as one argument.
The other lines make the script easier to use interactively:
@echo offhides the command text while leaving command results visible.pausekeeps the window open so you can read a success message or error.
Do not substitute a server’s web-management address or a local folder path for the UNC share path.
Step 3: Save, Run, and Test Drives.bat
- Save the document as
Drives.bat. Use the editor’s all-files save option rather than its text-document option. - Check that the filename is not actually
Drives.bat.txt. Enable filename-extension display in File Explorer if necessary. - Double-click the file while signed in as the Windows user who needs the drive.
- Read the command output before dismissing the window.
- Open the mapped drive in File Explorer and confirm that it contains the expected files.
Run this basic mapping script normally, not with administrator privileges. Elevated programs can use a different security context, so a drive mapped from an administrator command window may not appear in ordinary File Explorer.
Keep the file somewhere convenient if you intend to run it manually. Merely naming a file Drives.bat does not make Windows execute it at sign-in.
Map Multiple Shares or Use Another Account
Put one mapping command on each line for multiple shares, or use an asterisk to request another account’s password interactively.
Multiple Shared Folders
Put one mapping command on each line. For example, this batch file maps separate Finance and Projects shares:
@echo off
net use F: "\\fileserver\Finance"
net use G: "\\fileserver\Projects"
pause
Replace both UNC paths and choose available drive letters before saving the file. Run it as the intended Windows user, then open both mapped drives.
Check the result of each command. Batch files normally continue after a failed mapping, so a success message for the last drive does not prove that every earlier drive connected.
A Different Server Account
If the share needs another account, specify the username and use an asterisk in the password position to request the password interactively:
@echo off
net use I: "\\fileserver\share" * /user:DOMAIN\username
pause
Replace the share, drive letter, and account name, then save and run the file. Enter the password when prompted; Windows does not display it as you type. Open the mapped drive afterward to confirm access.
For an Active Directory account, use the appropriate domain-qualified username. A NAS or standalone server may require its own local-account format instead.
Do not replace the asterisk with a password in the batch file. Anyone who can read the file could read that password, and copying the script would copy the credential with it.
An interactive password prompt is unsuitable for unattended sign-in automation.
Run the Drive Mapping at Sign-In
For an administrator-managed setup, a Windows Run registry entry can launch a local mapping script whenever a user signs in. An entry under HKLM applies across users on that computer, so deploy it only when that scope is intended.
The password-free workflow for JumpCloud Commands is Windows-only and requires matching endpoint and file-server usernames and passwords. Its supported account-matching setup covers NTFS-permission file servers, Active Directory file servers, and NAS devices. Verify that requirement before relying on this workflow; it is not a requirement for every possible batch-file mapping method.
Create the Login-Script Generator
- Create a file named
setnetdrive.bat. In JumpCloud Commands’ workflow, the Run value namedmapdrivelaunches the generated mapping file atc:\scripts\netuse.bat. The adapted generator below creates the directory before writing the file and replaces only its designated drive mapping:@echo off if not exist c:\scripts ( mkdir c:\scripts ) if not exist c:\scripts exit /b 1 reg add hklm\software\microsoft\windows\currentversion\run /v mapdrive /t REG_SZ /d c:\scripts\netuse.bat /f if errorlevel 1 exit /b 1 ( echo @echo off echo net use f: /delete /yes echo net use f: "\\fileserver\share" /user:%%username%% ) > c:\scripts\netuse.bat - Replace the server address and share name. Reserve the chosen letter for this script; if you change it, change both the deletion line and the mapping line. The parenthesized group writes all its output into the generated file. The targeted deletion makes a repeat run replace that drive’s connection without removing unrelated mappings. If the drive is not connected yet, the deletion can report that the connection was not found; the following mapping command still runs.
- Adapt the account name if the server is an Active Directory member. JumpCloud Commands’ AD form places the domain before the escaped username variable. Replace the mapping line inside the parenthesized group with:
echo net use f: "\\fileserver\share" /user:DOMAIN\%%username%%For JumpCloud Commands’ example Finance share, the corresponding line is:
echo net use f: "\\10.10.10.10\Finance" /user:CONTOSO\%%username%%These are example server settings, not universal values. Confirm the actual UNC path and AD domain with the file-server administrator. The doubled percent signs belong in the generator: they preserve
%username%in the file being written so it expands for the user who later signs in. In a mapping batch file written directly, the environment-variable form uses single percent signs. - Keep unrelated connections intact. Avoid the following broad-reset line from JumpCloud Commands’ generator if other mapped drives or network connections must remain available:
echo net use * /delete /yes > c:\scripts\netuse.batThis line overwrites the generated file with a command that disconnects all of the executing user’s
net useconnections. The deletion happens when the generated script runs, not whenechowrites the line. The targeted deletion in the adapted generator avoids that blanket reset. - Deploy the generator, protect its output, and test a real user sign-in using one of the deployment options below.
Deploy Locally or Through Device Management
Save the login-script generator as setnetdrive.bat for either deployment route. For a local installation, run that file once with administrator privileges. Elevation is needed to create the machine-wide registry entry; it is not how the eventual user mapping should be tested.
For deployment through JumpCloud Commands:
- Open Device Management > Commands, click +, and choose Command.
- Give the command an identifiable name. Set Type to Windows and leave Windows PowerShell unchecked.
- In JumpCloud Commands’ Command field, enter:
cmd.exe /c c:\windows\temp\setnetdrive.bat - Under Launch Event, set Event to Manually so you can run the setup once per device.
- Under Files, upload
setnetdrive.bat. For this JumpCloud Commands workflow, set File Destination toc:\Windows\Temp\setnetdrive.bat. - Assign the intended Windows devices or device groups, save the command, and select Run Now.
After either deployment route, finish these checks:
- Confirm that the generated mapping file exists and contains the intended drive letter, share, and account expression.
- Apply the JumpCloud Commands requirement that the generated file be editable only by administrators. Give sign-in users read and execute access, not write access; also protect the containing directory against users replacing the file.
- Confirm that the intended user can authenticate to the share without an interactive password prompt.
- Sign out and sign back in as that user with the required network connection available.
- Open the mapped drive in that user’s File Explorer and verify its contents.
The device-management job runs the setup batch file when its Command field is set to cmd.exe /c c:\windows\temp\setnetdrive.bat. A mapping created in its administrative or service context would not prove that the desktop user has a mapped drive.
Leave pause out of the generated login script. It is useful for a manually launched file, but it would leave an unattended sign-in script waiting for input.
Fix Common Batch Drive-Mapping Errors
Use the error message to identify the cause before changing credentials or deleting connections.
| Symptom | Likely Cause | Targeted Fix |
|---|---|---|
| The local device name is already in use | The letter belongs to a local device or another mapping | Choose a free letter, or remove only a confirmed obsolete network mapping. |
| The network path cannot be found | Incorrect UNC path, unreachable server, or unavailable VPN | Verify the share path and network connection, then run the file again. |
| Access is denied | Wrong account or insufficient share/file-system permissions | Authenticate with an authorized account or have the share administrator correct access. |
| Windows rejects multiple connections using different usernames | An existing connection to the same server uses another account | Close affected files, disconnect the conflicting server connections, and reconnect using one account. |
| The script works manually but fails at sign-in | The network is not ready, credentials require interaction, or the script runs in the wrong context | Resolve the missing prerequisite and test another user sign-in. |
Replace an Obsolete Mapping
First confirm that the connection is no longer needed and close files opened through it. To remove a mapping assigned to I:, run:
net use I: /delete
For a connection listed by its UNC path rather than a drive letter, target that connection instead:
net use "\\fileserver\share" /delete
Use the actual connection shown on your computer. If the problem is conflicting credentials, inspect every connection to the affected server; removing one drive may leave another authenticated connection active.
After removing the relevant connection, run the mapping script again as the intended user and open the resulting drive.
Handle a Network That Is Unavailable at Sign-In
A Run registry entry starts a program; it does not guarantee that a VPN or file server is ready.
If the drive connects after you establish the VPN and manually run the batch file, the mapping command itself is probably sound. For reliable automation, use your organization’s approved sign-in or task mechanism to launch the script when the required connection is available.
Test that mechanism from a fresh sign-in, then open the share through the mapped drive. A successful manual run alone does not validate automatic startup behavior.
Confirm the Drive Mapping Works
A drive letter appearing in File Explorer is only part of the check. Confirm the destination from the same Windows account that will use it:
net use I:
Substitute your chosen letter, then verify:
- The reported remote path is the intended share.
- The drive opens and shows the expected folders.
- A file you are authorized to read opens successfully.
- If write access is required, you can create and remove a harmless test file.
- If sign-in automation was installed, the drive works after another sign-in with the required network available.
Keep a clear distinction between interactive and unattended scripts: the former can prompt; the latter must already have working authentication. Once the checks pass, use the manual file or the protected login setup that matches how the computer is used.
Frequently Asked Questions
How Do I Run a BAT File From Another BAT File?
Use call to run another batch file and then return to the calling script. If both files are in the same folder, put this line in the parent batch file:
call "%~dp0Drives.bat"
Run the parent file and confirm that the mapped drive opens. %~dp0 resolves to the parent batch file’s folder, so the call does not depend on the current working directory.
Can a Batch File Change My Network Home Folder Location?
A batch file can remap a drive letter to a different existing share, but that does not change an Active Directory home-folder setting or move your files. Changing the actual home-folder location requires updating the account configuration and arranging any necessary data migration and permissions. Confirm which change you need before replacing the drive mapping.
Does the File Have to Be Named Drives.bat?
No, Drives.bat is simply a descriptive filename; another filename with the .bat extension can execute the same commands. If a registry entry or another script launches the file, its reference must match the filename and location you actually use.
Can I map a network drive by using an IP address?
Yes. The documented Finance example uses 10.10.10.10 as the server portion of the UNC path, demonstrating that an IP address can identify the file server. It assigns that share to F: and uses a domain-qualified username expression for authentication.
