161

I need to be able determine the path of the network Q drive at work for a WEBMethods project. The code that I have before is in my configuration file. I placed single character leters inside of the directories just for security reasons. I am not sure what the semi-colon is for, but I think that the double slashes are were the drive name comes to play.

Question: Is there an easy way on a Windows 7 machine to find out what the full path of the UNC is for any specific drive location?

Code:

allowedWritePaths=Q:/A/B/C/D/E/
allowedReadPaths=C:/A/B;//itpr99999/c$/A/FileName.txt
allowedDeletePaths=
0

7 Answers 7

370

In Windows, if you have mapped network drives and you don't know the UNC path for them, you can start a command prompt (Start → Run → cmd.exe) and use the net use command to list your mapped drives and their UNC paths:

C:\>net use
New connections will be remembered.

Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           Q:        \\server1\foo             Microsoft Windows Network
OK           X:        \\server2\bar             Microsoft Windows Network
The command completed successfully.

Note that this shows the list of mapped and connected network file shares for the user context the command is run under. If you run cmd.exe under your own user account, the results shown are the network file shares for yourself. If you run cmd.exe under another user account, such as the local Administrator, you will instead see the network file shares for that user.

Sign up to request clarification or add additional context in comments.

6 Comments

Be careful you don't run cmd.exe as Administrator
@DaveMateer might be an idea to say why?
@theyetiman because if you run as administrator - your user specific mappings do not show. As I have just found out
This is so unnecessarily difficult
Thanks to Dave Mater - it only works when NOT running the CMD prompt as Administrator. Fooled me as I usually always run CMD prompts as Admin.
|
44

If you have Microsoft Office:

  1. RIGHT-drag the drive, folder or file from Windows Explorer into the body of a Word document or Outlook email
  2. Select 'Create Hyperlink Here'

The inserted text will be the full UNC of the dragged item.

2 Comments

What a practical and quick solution for all of us in corporate lock-down environments! Much appreciated workaround!
I never even new about the RIGHT-drag. It works like a charm! Thanks!
10
wmic path win32_mappedlogicaldisk get deviceid, providername

Result:

DeviceID  ProviderName
I:        \\server1\Temp
J:        \\server2\Corporate
Y:        \\Server3\Dev_Repo
Z:        \\Server3\Repository

As a batch file (src):

@if [%1]==[] goto :Usage
@setlocal enabledelayedexpansion
@set _NetworkPath=
@pushd %1
@for /f "tokens=2" %%i in ('wmic path win32_mappedlogicaldisk get deviceid^, providername ^| findstr /i "%CD:~0,2%"') do @(set _NetworkPath=%%i%CD:~2%)
@echo.%_NetworkPath%
@popd
@goto :EOF
:: ---------------------------------------------------------------------
:Usage
  @echo.
  @echo. Get the full UNC path for the specified mapped drive path
  @echo.
  @echo.  %~n0 [mapped drive path]

Example:

C:\> get-unc-path.bat z:\Tools\admin

\\EnvGeoServer\Repository\Tools\admin

Batch script adapted from https://superuser.com/a/1123556/16966. Please be sure to go vote that one up too if you like this solution.

Update 2021-11-15: bug fix. Previously the batch only reported drive letter UNC root and neglected to also report the folder path.

%CD% is set from %%i through some kind of CMD magic.
%CD:~0,2% and %CD:~2% extract the drive letter and trailing path substrings respectively. e.g. :~2% does '\Tools\admin' from 'Z:\Tools\admin'.

Comments

7

This question has been answered already, but since there is a more convenient way to get the UNC path and some more I recommend using Path Copy, which is free and you can practically get any path you want with one click:

https://pathcopycopy.github.io/

Here is a screenshot demonstrating how it works. The latest version has more options and definitely UNC Path too:

enter image description here

2 Comments

BTW, "Copy as path" comes built-in with Windows (at least 7 & 10) when you use Shift-Right Mouse Click to bring up the extended context menu. Alas, on a mapped drive this also does NOT include the UNC path, either. Only when you're looking at an UNC path in Explorer, it will copy the UNC path of the file.
I think using the net use that comes with the OS is more convenient than downloading a third-party tool.
6

The answer is a simple PowerShell one-liner:

Get-WmiObject Win32_NetworkConnection | ft "RemoteName","LocalName" -A

If you only want to pull the UNC for one particular drive, add a where statement:

Get-WmiObject Win32_NetworkConnection | where -Property 'LocalName' -eq 'Z:'  | ft "RemoteName","LocalName" -A

1 Comment

Thanks. Tried Get-WmiObject Win32_NetworkConnection | ft "RemoteName","LocalName" -A in Windows Powershell, and it worked. It's odd because I opened up Dos Cmd as Administrator , and net use did Not bring up anything. It's probably because my employer's company laptop that I use has some kind of restriciton.
0
$CurrentFolder = "H:\Documents"
$Query = "Select * from Win32_NetworkConnection where LocalName = '" + $CurrentFolder.Substring( 0, 2 ) + "'"
( Get-WmiObject -Query $Query ).RemoteName

OR

$CurrentFolder = "H:\Documents"
$Tst = $CurrentFolder.Substring( 0, 2 )
( Get-WmiObject -Query "Select * from Win32_NetworkConnection where LocalName = '$Tst'" ).RemoteName

Comments

0

my 5 cents: a powershell script when run via PowerShell will create a short cut pointing to PowerShell and itself such that you can drop files onto it to get the UNC path (or local normal file path) into the clipboard

https://inmood.ch/get-unc-path-of-fileserver-file/

# run without arguments will create a file called DropFileToGetUNCPath.lnk

# if you drop a file onto the shortcut it'll return the UNC path

if($args[0] -eq $null)

{

            # creating the shortcut to drop files later on

            $path = $pwd.path

            $script = $MyInvocation.MyCommand.Path

            $WshShell = New-Object -comObject WScript.Shell

            $Shortcut = $WshShell.CreateShortcut("$path\DropFileToGetUNCPath.lnk")

            $Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

            $Shortcut.Arguments = "-noprofile -file """ + $script + """"

            $Shortcut.Save()

}else{

            $file = $args[0]

}

 

$drive = $pwd.drive.name + ":"

# find UNC paths for directories

$drives = net use

$drive = ($drives -match ".*" + $drive + ".*")

#debug

#echo $drive

$parts = $drive -split "\s{1,11}"

#debug

#echo $parts

$windowsDrive = $parts[1]

$uncDrive = $parts[2]

$file -replace $windowsDrive, $uncDrive | clip

1 Comment

if you put the Shortcut.lnk file into the "Send To" folder (find it by run: shell:sendto) you'll be able to "send" the file and receive the UNC path in the clipboard.. neat

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.