Get-PathRelativeTo

Converts a path to a relative path from a given source.

Syntax

Get-PathRelativeTo [-From] <Object> [[-FromType] <String>] [-To <Object>] [<CommonParameters>]

Description

The .NET framework doesn't expose an API for getting a relative path to an item. This function uses Win32 APIs to call PathRelativePathTo.

Neither the From or To paths need to exist.

Related Commands

Parameters

Name Type Description Required? Pipeline Input Default Value
From Object The source where from where the relative path will be calculated. Can be a string or an file system object. true false
FromType String Whether the from/source path is a file or a directory. The default is a directory. false false
To Object The path to convert to a relative path. It will be relative to the value of the From parameter. false true (ByValue)

EXAMPLE 1

Get-PathRelativeTo -From 'C:\Windows\system32' -To 'C:\Program Files'

Returns ..\..\Program Files.

EXAMPLE 2

Get-ChildItem * | Get-PathRelativeTo -From 'C:\Windows\system32'

Returns the relative path from the C:\Windows\system32 directory to the current directory.

EXAMPLE 3

Get-PathRelativeTo -From 'C:\I\do\not\exist' -To 'C:\I\do\not\exist\either'

Returns .\either.

EXAMPLE 4

Get-PathRelativeTo -From 'C:\I\do\not\exist' -FromType 'File' -To 'C:\I\do\not\exist\either'

Treats C:\I\do\not\exist as a file, so returns a relative path of .\exist\either.