Skip to main content

How to View Network Drive Mappings in Batch Script

Knowing which network drives are currently mapped for a user is a common requirement for diagnostic, inventory, and login scripts. A script might need to check if a specific drive letter is already in use before attempting to map a new one, or it might need to list all current connections for a troubleshooting report. The standard, built-in command-line tool for viewing and managing all network connections is NET USE.

This guide will teach you how to use the NET USE command to display a list of all currently mapped network drives and how to parse its output to use this information effectively in a batch script.

The Core Command: NET USE

The NET USE command is the primary tool for managing network resources. When run with no arguments, its main function is to list all of the current user's mapped network drives and other connections.

Syntax: NET USE

This simple command provides a complete list of all active network drive mappings for the current user's session.

Basic Example: Displaying All Mapped Drives

Running NET USE in a command prompt gives you an instant, human-readable report.

@ECHO OFF
ECHO --- Current Network Drive Mappings ---
ECHO.
NET USE
ECHO.
ECHO --- End of List ---

The NET USE command produces a table with four key columns, as output:

New connections will be remembered.

Status Local Remote Network
-------------------------------------------------------------------------------
OK H: \\FileServer\Home\jdoe Microsoft Windows Network
OK S: \\FileServer\Shared\Public Microsoft Windows Network
Disconnected T: \\OldServer\Archive Microsoft Windows Network
The command completed successfully.
  • Status: Indicates the current state of the mapping.
    • OK: The drive is connected and accessible.
    • Disconnected: The mapping exists but is not currently connected (e.g., the server is offline or you are not on the corporate network).
  • Local: The local device name, which is the drive letter (e.g., H:).
  • Remote: The full UNC path (\\Server\Share) that the drive letter is mapped to.
  • Network: The network provider that established the connection.

How to Check for a Specific Mapped Drive Letter

For scripting, you often don't need the whole list; you just need to know if a specific drive letter is already in use. You can do this in two excellent ways.

Method 1: Parsing NET USE (Good)

You can pipe the output of NET USE to FIND and search for the drive letter. FIND will set %ERRORLEVEL% to 0 if it finds a match.

@ECHO OFF
SET "DRIVE_LETTER=S:"
NET USE | FIND "%DRIVE_LETTER%" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO The drive letter %DRIVE_LETTER% is currently in use.
) ELSE (
ECHO The drive letter %DRIVE_LETTER% is available.
)

Method 2: The IF EXIST Trick (Better and Simpler)

This is a cleaner and more direct method. Windows treats a mapped drive letter as an existing object in the file system. You can check for its existence just like a folder.

@ECHO OFF
SET "DRIVE_LETTER=S:"

IF EXIST "%DRIVE_LETTER%\" (
ECHO The drive letter %DRIVE_LETTER% is currently in use.
) ELSE (
ECHO The drive letter %DRIVE_LETTER% is available.
)
note

The trailing backslash (\) is important to ensure you are checking for a drive/directory object.* This is the recommended method for checking if a drive letter is taken.

Common Pitfalls and How to Solve Them

Problem: The Output is Hard to Parse

The table format of NET USE is great for humans but can be tricky for a script to parse if you need to extract the UNC path, as the Remote column can contain spaces and is not at a fixed position.

Solution: For extracting the UNC path, WMIC is a more reliable tool. However, for simply listing or checking for a drive's existence, the NET USE | FIND or IF EXIST methods are perfectly sufficient.

Problem: Checking for Mapped Drives from an Elevated Prompt

This is a critical concept to understand. Mapped drives are part of a user's logon session. If a user has an S: drive mapped and you open a new command prompt by right-clicking and selecting "Run as administrator," this new elevated session is a different logon session.

Exmaple of situation with error:

  • The user can see the S: drive in File Explorer.
  • You open an Administrator cmd.exe window.
  • You run NET USE. The list is empty.

Solution: This is expected Windows security behavior. A standard user's drives are not available to an elevated session by default. To work around this, your script must either:

  • Run as the standard user (if it doesn't need admin rights for other tasks).
  • Remap the drive within the elevated script's context if it needs to access it.

Practical Example: A "Prerequisite Check" Script

This script needs to map a software repository to the R: drive. Before it does, it uses the recommended IF EXIST method to check if R: is already in use.

@ECHO OFF
SETLOCAL
SET "RepoDrive=R:"
SET "RepoPath=\\FileServer\Software\Installers"

ECHO --- Software Installer Script ---
ECHO Checking for required drive letter %RepoDrive%...

IF EXIST "%RepoDrive%\" (
ECHO [ERROR] Drive letter %RepoDrive% is already in use by another mapping.
ECHO Please disconnect it and run this script again.
NET USE %RepoDrive%
GOTO :End
)

ECHO [SUCCESS] Drive letter is available.
ECHO Mapping %RepoPath% to %RepoDrive%...
NET USE %RepoDrive% "%RepoPath%"

ECHO.
ECHO --- Setup complete ---
:End
ENDLOCAL

Conclusion

The NET USE command is the definitive tool for viewing your current network drive mappings from the command line.

Key takeaways for using it in scripts:

  • Run NET USE with no arguments to get a list of all current connections.
  • The IF EXIST "Z:\" trick is the simplest and most reliable method for checking if a specific drive letter is already in use.
  • Be aware that network drives are specific to a logon session; an elevated (Administrator) prompt will not see a standard user's mapped drives.