Skip to main content

How to Get the UNC Path of a Mapped Drive in Batch Script

Mapped network drives provide a convenient drive letter (like Z:) for accessing a network share. However, in scripting and automation, it's often more reliable to use the full UNC (Universal Naming Convention) path, which looks like \\Server\Share. A UNC path is explicit and doesn't depend on the user having a specific drive letter mapped. A common scripting task is to resolve a drive letter back to its original UNC path.

This guide will teach you two primary methods for discovering the UNC path. We'll cover the classic approach using the NET USE command and the modern, more easily parsable method using WMIC. For most scripting needs, the WMIC method is superior.

The Challenge: Translating Letters to Paths

There is no direct variable like %Z_UNC_PATH% that holds this information. A script must query the system to find the mapping between the drive letter and the network share it points to. Both NET USE and WMIC can provide this information, but in different formats.

The Classic Method: Parsing NET USE

The NET USE command, when run without arguments, lists all current network connections. We can then filter this output to find the line corresponding to our drive letter.

For example, run the command:

C:\> NET USE

The output is formatted for humans, with a header and columns.

New connections will be remembered.

Status Local Remote Network
-------------------------------------------------------------------------------
OK Z: \\FileServer\UserData Microsoft Windows Network
The command completed successfully.

To get the UNC path for Z:, we need to find the line containing "Z:" and extract the third token.

@ECHO OFF
SET "DRIVE_LETTER=Z:"
SET "UNC_PATH="

FOR /F "tokens=2,3" %%A IN ('NET USE') DO (
IF /I "%%A"=="%DRIVE_LETTER%" (
SET "UNC_PATH=%%B"
GOTO :Found
)
)

:Found
IF DEFINED UNC_PATH (
ECHO The UNC path for %DRIVE_LETTER% is %UNC_PATH%
) ELSE (
ECHO Could not find a mapping for %DRIVE_LETTER%.
)

A far cleaner and more reliable method is to use WMIC (Windows Management Instrumentation Command-line) to query the Win32_MappedLogicalDisk class. This returns data in a more structured format that is easier to parse.

@ECHO OFF
SET "DRIVE_LETTER=Z:"
SET "UNC_PATH="

REM WMIC returns extra blank lines, so we pipe to FINDSTR to get just the line with data.
FOR /F "tokens=*" %%P IN (
'WMIC PATH Win32_MappedLogicalDisk WHERE "DeviceID='%DRIVE_LETTER%'" GET ProviderName ^| FINDSTR /I "\\\\"'
) DO (
SET "UNC_PATH=%%P"
)

IF DEFINED UNC_PATH (
ECHO The UNC path for %DRIVE_LETTER% is %UNC_PATH%
) ELSE (
ECHO Could not find a mapping for %DRIVE_LETTER%, or it is not a network drive.
)

How the WMIC Script Works

  • WMIC PATH Win32_MappedLogicalDisk: This tells WMIC which object class we want to query.
  • WHERE "DeviceID='%DRIVE_LETTER%'" : This is the filter. It selects only the object where the DeviceID (the drive letter) matches our target.
  • GET ProviderName: This tells WMIC to return only the ProviderName property, which is the UNC path.
  • | FINDSTR /I "\\\\": The output of WMIC includes a header and extra blank lines. We pipe the output to FINDSTR and search for a line containing \\ (the backslashes must be escaped). This effectively isolates the UNC path and discards the rest, making the FOR /F loop much cleaner.

Common Pitfalls and How to Solve Them

Problem: The Drive is Not a Network Drive

What happens if you run these scripts on C:?

  • NET USE method: The IF condition will never be true, and the script will correctly report that it couldn't find a mapping.
  • WMIC method: The WMIC query will return no results, and the script will also correctly report that no mapping was found.

Both methods handle this case gracefully. The WMIC method is slightly more explicit because it's querying a class specifically for mapped logical disks.

Problem: Parsing NET USE Can Be Unreliable

The output format of NET USE can change slightly between Windows versions or languages. It is formatted for human eyes, not for scripts.

Solution: The WMIC method is far more robust because its output is more structured and predictable. GET ProviderName will always return the same data type. For this reason, WMIC is the recommended approach for any script that needs to be reliable.

Practical Example: A Script to Unmap and Remap with a UNC Path

This script is useful in an environment where you want to ensure a connection is fresh. It resolves a drive letter to its UNC path, disconnects it, and then immediately remaps it using the more explicit UNC path.

@ECHO OFF
SETLOCAL
SET "DRIVE_LETTER=Z:"
SET "UNC_PATH="

ECHO --- Refreshing Network Drive Mapping for %DRIVE_LETTER% ---
ECHO.
ECHO Step 1: Resolving current UNC path...

FOR /F "tokens=*" %%P IN (
'WMIC PATH Win32_MappedLogicalDisk WHERE "DeviceID='%DRIVE_LETTER%'" GET ProviderName ^| FINDSTR /I "\\\\"'
) DO (
SET "UNC_PATH=%%P"
)

IF NOT DEFINED UNC_PATH (
ECHO [ERROR] %DRIVE_LETTER% is not a mapped network drive.
GOTO :End
)

ECHO Found UNC Path: %UNC_PATH%
ECHO.

ECHO Step 2: Disconnecting the drive letter...
NET USE %DRIVE_LETTER% /DELETE

ECHO Step 3: Remapping the drive using the full UNC path...
NET USE %DRIVE_LETTER% "%UNC_PATH%"

ECHO.
ECHO --- Refresh complete ---
:End
ENDLOCAL

Conclusion

Resolving a mapped drive letter back to its UNC path is a common requirement for creating robust, explicit scripts.

  • The NET USE command can provide the information, but its output is formatted for humans and can be tricky to parse reliably.
  • The WMIC PATH Win32_MappedLogicalDisk command is the modern and recommended method. It provides structured output that is easy to filter and capture, making your scripts more reliable and easier to maintain.

By using WMIC to find the true network path, you can make your scripts independent of pre-existing drive mappings and more explicit in their actions.