143

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=
Lachlan Dowding
  • 4,356
  • 1
  • 15
  • 18
Doug Hauf
  • 3,025
  • 8
  • 46
  • 70

7 Answers7

347

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.

Mike
  • 14,010
  • 29
  • 101
  • 161
Lachlan Dowding
  • 4,356
  • 1
  • 15
  • 18
  • 17
    Be careful you don't run cmd.exe as Administrator – Dave Mateer Jul 15 '14 at 10:50
  • 1
    @DaveMateer might be an idea to say why? – theyetiman Apr 13 '17 at 09:13
  • 10
    @theyetiman because if you run as administrator - your user specific mappings do not show. As I have just found out – Andez May 03 '17 at 08:18
  • 3
    This is so unnecessarily difficult – Fueled By Coffee Apr 08 '20 at 21:02
  • 1
    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. – AH. Nov 16 '21 at 07:17
  • @s31064 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. – crazyTech Nov 25 '21 at 14:33
41

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.

dlauzon
  • 1,241
  • 16
  • 23
8
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'.

matt wilkie
  • 17,268
  • 24
  • 80
  • 115
6

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

Ibo
  • 4,081
  • 6
  • 45
  • 65
  • 8
    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. – mgaert Jun 08 '18 at 09:27
  • 6
    I think using the `net use` that comes with the OS is more convenient than downloading a third-party tool. – shoover Oct 29 '19 at 22:03
5

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
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
s31064
  • 147
  • 2
  • 4
  • 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. – crazyTech Nov 25 '21 at 14:32
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
dieseyer
  • 11
  • 2
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
iPinky7
  • 54
  • 1
  • 3
  • 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 – iPinky7 Sep 24 '22 at 17:52