How to Map a Network Drive Using a BAT File

To map a network drive using a BAT file, put a net use command in a .bat file and run it under your normal Windows account. The command assigns a drive letter to an existing shared folder, with options for reconnecting at sign-in and using different credentials. Start with the basic script, then add only the authentication or startup behavior you need.

What Mapping a Network Drive Does

A mapped network drive gives a shared folder an address such as Z: on your computer. Programs can then access files through that drive letter instead of the folder’s full network path.

For example, a mapping can make this shared folder:

\\fileserver\Projects

appear as Z: in File Explorer.

The network address is a UNC path: two leading backslashes, a server name, and a share name. Mapping does not copy the files to your computer, create the shared folder, or grant access to it.

Before running one, you need:

  • The correct UNC path.
  • An unused drive letter.
  • Permission to access the share.
  • A connection to the server’s network, including your organization’s VPN when required.

How to Create a BAT File to Map a Network Drive

Choose the authentication approach before creating the file.

Your Situation Authentication to Use
Your Windows account already has access Use the current account; keep credentials out of the script.
The share requires another account Specify that account and prompt for its password.
The mapping must run unattended Use an authorized sign-in account or an IT-approved stored credential; avoid password prompts.

Step 1: Confirm the Share Path and Drive Letter

  1. Connect to the appropriate network or VPN.
  2. Open File Explorer and enter the full UNC path in its address bar.
  3. Open a folder or file you are authorized to access.
  4. Check This PC and choose an unused drive letter. The examples here use Z:.

A server name by itself is not a complete drive-mapping target. You need a shared folder, such as \\fileserver\Projects, rather than just \\fileserver.

If you prefer to validate the connection through the mapping dialog, the common Windows 11 path is This PC > … > Map network drive. The ellipsis is the three-dot menu; confirm the File Explorer command layout for your installed Windows version if it differs. This layout is documented by Nova Southeastern University.

In that dialog:

  1. Select an available drive letter and enter the share’s path in Folder.
  2. Select Reconnect at sign-in if Windows should restore the mapping after you sign in.
  3. Select Connect using different credentials if the share requires an account other than your current Windows account.
  4. Click Finish, supply credentials if requested, and open the mapped folder to confirm access.
  5. Before testing your BAT file with that letter, close files using the test mapping, right-click the mapped drive under This PC, and choose Disconnect.

For University of Maryland School of Medicine’s SOMFiles service, you can enter \\somfiles.som.umaryland.edu in Folder and click Browse to locate an authorized folder. If you can access a specific folder but cannot browse its parent, enter the full folder path instead, as explained in the UMSOM mapping instructions. Other organizations use different server addresses; confirm the full network path with the share owner or IT staff.

Step 2: Write the BAT File

Open Notepad and paste this script:

@echo off
net use Z: "\\fileserver\Projects" /persistent:yes
if errorlevel 1 (
    echo Drive mapping failed. Review the Windows error above.
    pause
    exit /b 1
)
echo Drive Z: is mapped.
pause

Replace \\fileserver\Projects with your actual share path. Change Z: if that letter is already in use.

Here is what the important parts do:

  • @echo off keeps the window from displaying every batch-file instruction.
  • net use creates the network connection.
  • /persistent:yes tells Windows to save the mapping and attempt to restore it at future sign-ins.
  • if errorlevel 1 detects a failed command and prevents the script from displaying a success message.
  • pause keeps the window open so you can read the result.

Keep quotation marks around the share path, especially when a folder name contains spaces.

For a mapping that Windows should not save for future sign-ins, replace the mapping line with:

net use Z: "\\fileserver\Projects" /persistent:no

That setting does not disconnect the drive when the BAT window closes. It controls whether Windows remembers the new mapping.

Step 3: Save, Run, and Test the File

  1. In Notepad, choose File > Save As.
  2. Set Save as type to All files.
  3. Save the file as MapDrive.bat. Confirm that it was not saved as MapDrive.bat.txt.
  4. Double-click it from your normal desktop session. Do not select Run as administrator for an ordinary File Explorer mapping.
  5. Read the command’s result, then open This PC.
  6. Open Z: and a known file or folder inside it.

You can also inspect the mapping from Command Prompt:

net use Z:

Check that the displayed remote path is the intended share. If the command fails, keep its error message visible and diagnose that error before adding more commands to the script.

Use Different Credentials or Share Addresses

Map a Drive With a Different Account

Use this complete version when the share requires another account:

@echo off
net use Z: "\\fileserver\Projects" * /user:CONTOSO\alice /persistent:yes
if errorlevel 1 (
    echo Drive mapping failed. Review the Windows error above.
    pause
    exit /b 1
)
echo Drive Z: is mapped.
pause

Replace CONTOSO\alice with the account your organization provides. A server-local account may use a form such as FILESERVER\alice; use the identity that the server actually recognizes.

The asterisk requests an interactive password prompt. The password is not displayed as you type and is not written into the BAT file.

Save the file, run it normally, enter the password, and open Z: to confirm access. This version is intended for interactive use, not an unattended startup task.

Do not replace the asterisk with a password in a shared script. Anyone who can read that file could read the credential. For unattended access, use your authorized Windows identity or credentials stored through your organization’s approved process.

Map a Drive Using an IP Address

A UNC path can use an IP address instead of a server name. For example:

net use Z: "\\192.168.1.50\Projects" /persistent:yes

Replace the example address and share name with the server’s actual values. Run the command from a normal Command Prompt, check its result, and open Z: in File Explorer.

An IP-based path does not bypass permissions or authentication. Prefer the organization-provided hostname when available: IP addresses can change, and some Windows authentication configurations depend on server names.

Map an Organization-Specific User Folder

Some organizations construct a personal share path from the signed-in username.

UMSOM’s documented user-drive example uses U: and \\somfiles.som.umaryland.edu\users\%username%. For an authorized UMSOM account, the batch-file command is:

net use U: "\\somfiles.som.umaryland.edu\users\%username%" /persistent:yes

Inside a BAT file, %username% expands to the account running the script. It does not automatically become the name supplied through alternate network credentials.

Confirm the assigned drive letter and UNC path in your organization’s file-share documentation. After running the command, open U: and verify that it contains your expected files.

Run the BAT File at Sign-In

A startup shortcut can run a working BAT file at sign-in, while the reconnect helper restores saved unavailable mappings.

Run a BAT File From Your Startup Folder

For a basic mapping, Windows’ saved reconnection behavior may be enough. Use a startup BAT file when you specifically need the script to run each time you sign in.

  1. Confirm that the BAT file works when run manually.
  2. Remove the pause lines so startup does not leave a window waiting for input.
  3. Keep the file in a stable local folder, not on the network drive it is supposed to connect.
  4. Press Windows+R and enter:
    shell:startup
  5. Place a shortcut to the BAT file in the folder that opens.
  6. Sign out and sign back in with the required network available.
  7. Open the mapped drive and verify access.

A startup script can run before a manually established VPN connection. In that situation, connect the VPN and run the BAT file afterward rather than repeatedly changing a valid share path.

Administrators can also include a batch file in a domain sign-in script, allowing an organization to apply network-drive mappings when users sign in.

Reconnect Saved Drives When the Network Is Late

Use a reconnect helper only when Windows already has a saved mapping but marks it unavailable after sign-in. It will not fix an incorrect share path.

Microsoft’s reconnect-script workaround places MapDrives.ps1 in %SystemDrive%\Scripts\ and uses a 30-second delay between mapping retries. The simplified helper below follows that approach, with an initial reconnect attempt and a delayed retry if it fails.

Use this only where PowerShell script execution is allowed by your organization’s policy.

  1. Create the Scripts folder at the root of the Windows system drive if necessary, then save the following PowerShell script as MapDrives.ps1 in that folder. Administrative approval may be required to create or write to that folder.
  2. Save the following as MapDrives.ps1 in %SystemDrive%\Scripts\:
    $ErrorActionPreference = 'Stop'
    function Restore-UnavailableDrives {
        $failed = $false
        $mappings = Get-SmbMapping |
            Where-Object { $_.Status -eq 'Unavailable' }
        foreach ($mapping in $mappings) {
            try {
                New-SmbMapping `
                    -LocalPath $mapping.LocalPath `
                    -RemotePath $mapping.RemotePath `
                    -Persistent $true `
                    -ErrorAction Stop | Out-Null
            }
            catch {
                Write-Warning $_.Exception.Message
                $failed = $true
            }
        }
        return (-not $failed)
    }
    if (-not (Restore-UnavailableDrives)) {
        Start-Sleep -Seconds 30
        if (-not (Restore-UnavailableDrives)) {
            exit 1
        }
    }
    exit 0
  3. Save this launcher as ReconnectDrives.bat in a stable local folder:
    @echo off
    powershell.exe -NoProfile -File "%SystemDrive%\Scripts\MapDrives.ps1" >> "%TEMP%\MapDrives.log" 2>&1
  4. Run the launcher normally and inspect MapDrives.log in your temporary-files folder for errors. Open the mapped drive to test access. The helper reconnects saved unavailable mappings; it does not create missing ones.
  5. Open Run and enter:
    shell:startup
  6. Add a shortcut to the launcher. Replace any startup shortcut that would run the ordinary mapping BAT file at the same time.
  7. Sign out and back in, then open the drive and check the log if it remains unavailable.

Do not weaken PowerShell execution policy merely to make this workaround run. If policy blocks the script, use the BAT file after connecting to the network or ask IT to deploy an approved reconnect method.

Fix Common BAT Drive-Mapping Errors

Match the actual error or behavior before deleting mappings or changing credentials.

Error or Symptom Likely Cause Next Action
“The local device name is already in use” The requested drive letter is occupied Inspect the existing mapping; choose another letter or deliberately replace it.
“The network path was not found” Incorrect server path, unavailable network, or name-resolution problem Test the exact UNC path in File Explorer and confirm the network or VPN connection.
“Access is denied” or a sign-in failure Incorrect identity or insufficient permission Confirm the account and ask the share owner to verify access.
“Multiple connections … using more than one user name” An existing connection to the server uses another account Identify and disconnect only the conflicting connection before retrying.
BAT reports success, but Explorer has no drive The script ran under another account or an elevated session Run it in the same normal user session as File Explorer.
It works manually but not at sign-in Network readiness or execution-context difference Check when the network becomes available and which account runs the script.
Double-clicking opens Notepad The file may still have a .txt extension Confirm the actual filename ends in .bat.

Replace an Occupied Drive Letter Safely

First inspect current network connections:

net use

If the intended letter already points to the correct share, you may not need to recreate it. A simple creation script is not necessarily safe to rerun blindly against an existing mapping.

If Z: points to an obsolete or unwanted share:

  1. Close files and applications using Z:, then right-click that mapped drive under This PC and choose Disconnect.
  2. Remove that specific connection:
    net use Z: /delete
  3. Run your mapping BAT file.
  4. Open Z: and confirm that it now points to the intended folder.

If the letter belongs to a local disk or another device rather than a network mapping, select a different letter.

Avoid putting a wildcard deletion command into a general-purpose mapping script. Removing every network connection can interrupt unrelated work.

Resolve a Conflicting Server Login

Windows can reject a connection when your session is already connected to the same server under another username. That existing connection may not have a drive letter.

  1. List connections:
    net use
  2. Identify the connection to the affected server and close files using it.
  3. Disconnect the specific conflicting share, substituting its actual path:
    net use "\\fileserver\OtherShare" /delete
  4. If other connections to that server use the conflicting identity, close and disconnect those deliberately as well.
  5. Run the credential-prompt BAT file and open the resulting drive.

Do not work around this by putting several passwords into the script. Decide which authorized identity should access that server during the session.

Confirm the Mapping and Keep It Working

A successful command is only part of the check. Confirm that the mapping works where you intend to use it:

  • The drive appears in your normal File Explorer session.
  • Its remote path is correct.
  • You can open the expected files.
  • The application that needs the drive can access it.
  • If reconnection matters, access works after a fresh sign-in with the network available.

Mapped letters are tied to a user and logon context. For a service, deployment process, or background task, an explicit UNC path is often more predictable than assuming it can see your desktop’s mapped drive.

Once the manual BAT file works, add startup automation only if you need it. Keep the script free of plaintext passwords and retest it whenever the share address or required account changes.

Frequently Asked Questions

Can I Open a Network Folder From a BAT File or Shortcut?

Yes: a BAT file can open a shared folder without assigning a drive letter:

@echo off
start "" "\\fileserver\Projects"

Replace the UNC path, save the file, and run it; the empty quotation marks supply the window-title argument expected by start. You can also create a Windows shortcut whose target is the UNC path, provided your account already has access.

Can a BAT File Set a Drive Name Instead of a Drive Letter?

A drive letter and a display name are different things: net use assigns the letter and connects it to a share. A friendly name displayed in File Explorer does not replace the UNC path or identify the server for the command. Use an available letter for the mapping and the actual network path as its target.

Does Mapping a Network Drive Create the Shared Folder?

No. Mapping connects your computer to a share that already exists. The server or NAS must have the folder shared, and your account must have the appropriate permissions before the BAT file can connect successfully. If either is missing, have the share owner configure access, then run the mapping script and open the drive to verify it.