Converting a path to UNC path doesn’t really take that many lines of code, but having recently been in a project where I needed to do this a lot, I got tired of copy-pasting the same 2-3 lines of code back and forth, and decided to create a helper function out of it.

It’s by no means perfect, and at the moment only deals with UNC paths where you have a local path and a computer name, where you want a UNC path in the format of \\computer\drive$\path\to\somewhere\

Hopefully it will be helpful to someone else as well.

function ConvertTo-UNCPath {
Convert a path to UNC path
Convert a path to UNC path
Author: Øyvind Kallstad
Date: 26.10.2015
Version: 1.0
param (
[Parameter(Position = 2, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[string] $Path,
[Parameter(Position = 1)]
[string] $ComputerName
$pathRoot = [System.IO.Path]::GetPathRoot($Path)
Write-Output ("\\$($ComputerName)$(($Path).Replace($pathRoot, "\$($Path[0])$\"))")


    1. haha.. yes, I know what you mean. Did it for the longest time, before I got fed up with it. Glad you liked it! 😀


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s