Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

UNC path from local path in PowerShell

Use this simple PowerShell code to convert a local path into an UNC path.

I recently needed to determine an Universal Naming Convention (UNC) path from a local path on my Windows Machine.

An UNC path will be in the format \\servername\whatever\path\to\files whilst a local path will be in the format g:\path\to\files. Whilst both point to the same location containing the same files, sometimes there is a need to avoid drive letters for certain applications.

To determine the UNC path for any locally mapped network drive, open PowerShell and cd to the path in question. (For example cd "g:\path\to\files").

Paste the following code and press enter. The UNC path will then be displayed beside the local path.

$Drive = $(get-location).Path
$x = new-object system.io.driveinfo($Drive)
$x.drivetype | Out-Null

If ($x.drivetype -eq "Fixed") {
Write-Host "`r`n`r`nSorry," $Drive "is not a networked drive."
}
Else {
$currentDirectory = Get-Location
$currentDrive = Split-Path -qualifier $currentDirectory.Path
$logicalDisk = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$currentDrive'"
$unc = $currentDirectory.Path.Replace($currentDrive, $logicalDisk.ProviderName)
$output = "`r`n`r`nThe UNC Path for " + $currentDirectory + " is " + $unc
$output
}

If a local drive is not mapped to a server (such as C:\ or D:\), the user will be advised.

This script is also available on Github Gist.

Making a standalone utility

The above code can be further modified and adapted into a nifty standalone PowerShell script that can be executed from a batch file.

The following script will enable a user to navigate to a specific file via a Windows file dialogue box and use this information to generate its UNC path. The UNC path will be automatically copied to the clipboard. Pressing ENTER will terminate the script thus making this a very useful utility.

# UNC File Path Finder
# Written by Adam Dimech
# Based on https://code.adonline.id.au/unc-path-from-local-path-in-powershell/
# 1 March 2017

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false # Only one file can be chosen
	InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"; #Opens in My Computer
	Filter = 'All File Types|*.*' # Specified file types
}
 
Write-Host -ForegroundColor Cyan "`r`nUNC Path Identifier."
Write-Host -ForegroundColor Yellow "Created by Adam Dimech. 1 March 2017." 
Write-Host "Based on https://code.adonline.id.au/unc-path-from-local-path-in-powershell/" 
Write-Host "`r`nChoose a file from the dialogue box to determine its UNC path..." 
 
[void]$FileBrowser.ShowDialog()

$path = $FileBrowser.FileNames;

If($FileBrowser.FileNames -like "*\*") {

	#Do something to the file selected

	foreach($file in Get-ChildItem $path){
	Get-ChildItem ($file) |	ForEach-Object {
		
	# Determine the UNC paths
			
	$Drive = Get-Childitem $path
	$x = new-object system.io.driveinfo($Drive)
	$x.drivetype | Out-Null

	If ($x.drivetype -eq "Fixed") {
	Write-Host "`r`n`r`nSorry! " -f red -nonewline; Write-Host $Drive "is not located on a networked drive." -f white;
	}
	Else {
	
	$origpath = Get-Location # Collect the current path
	
	cd $file.DirectoryName
	$currentDirectory = Get-Location
	$currentDrive = Split-Path -qualifier $currentDirectory.Path
	$logicalDisk = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$currentDrive'"
	$unc = $currentDirectory.Path.Replace($currentDrive, $logicalDisk.ProviderName)
	
	cd $origpath # Return to the original path
	
	Write-Host -ForegroundColor White "`r`n`r`nThe UNC Path for:"
	Write-Host -ForegroundColor Cyan $path
	Write-Host -ForegroundColor White "`r`nis:"
	$output = $unc+"\"+$File.Name #The UNC path and file name output
	$output | Clip #Sends file path to clipboard
	Write-Host -ForegroundColor Cyan $output
	Write-Host "`r`nThe UNC file path has been copied to your clipboard."
	}
	}
	}
}

else {
    Write-Host "Cancelled by user"
}

Read-Host -Prompt "`r`nPress Enter to exit..."

The above PowerShell code has been posted on Github Gist.

   

Comments

2 responses to “UNC path from local path in PowerShell”

On 29 January 2020, s31064 wrote: Hyperlink chain icon

Wouldn’t it be a lot simpler to just use Win32_NetworkConnection? Now it becomes a one-liner:
Get-WmiObject Win32_NetworkConnection | where -Property ‘LocalName’ -eq ‘Z:’ | ft “RemoteName”,”LocalName” -A

Reply

On 4 February 2020, diseyer wrote: Hyperlink chain icon


$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

Reply

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.