Resolve-RelativePath

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

Syntax

Resolve-RelativePath [-Path <String>] -FromDirectory <String> [<CommonParameters>]

Resolve-RelativePath [-Path <String>] -FromFile <String> [<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
Path String

The path to convert to a relative path. It will be relative to the value of the From parameter.

false true (ByPropertyName)
FromDirectory String

The source directory from which the relative path will be calculated. Can be a string or an file system object.

true false
FromFile String

The source directory from which the relative path will be calculated. Can be a string or an file system object.

true false

EXAMPLE 1

Resolve-RelativePath -Path 'C:\Program Files' -FromDirectory 'C:\Windows\system32'

Returns ..\..\Program Files.

EXAMPLE 2

Get-ChildItem * | Resolve-RelativePath -FromDirectory 'C:\Windows\system32'

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

EXAMPLE 3

Resolve-RelativePath -Path 'C:\I\do\not\exist\either' -FromDirectory 'C:\I\do\not\exist'

Returns .\either.

EXAMPLE 4

Resolve-RelativePath -Path 'C:\I\do\not\exist\either' -FromFile 'C:\I\do\not\exist_file'

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