Skip to content

UNC path from local path in PowerShell

G23rd February 2017

CNo Comments

TPowerShell, Windows

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$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

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
# 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" 
Write-Host "`r`nChoose a file from the dialogue box to determine its UNC path..." 

$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$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.



No comments have yet been submitted. Be the first!

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.