Skip to content

Instantly share code, notes, and snippets.

Last active March 7, 2017 03:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Select a file to determine its UNC path
# 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..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment