Converts a path to a relative path from a given source.
Get-PathRelativeTo [-From] <Object> [[-FromType] <String>] [-To <Object>] [<CommonParameters>]
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.
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) |
Get-PathRelativeTo -From 'C:\Windows\system32' -To 'C:\Program Files'
Returns ..\..\Program Files
.
Get-ChildItem * | Get-PathRelativeTo -From 'C:\Windows\system32'
Returns the relative path from the C:\Windows\system32
directory to the current directory.
Get-PathRelativeTo -From 'C:\I\do\not\exist' -To 'C:\I\do\not\exist\either'
Returns .\either
.
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
.