Converts a path to a relative path from a given source.
Resolve-RelativePath [-Path <String>] -FromDirectory <String> [<CommonParameters>]
Resolve-RelativePath [-Path <String>] -FromFile <String> [<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 |
---|---|---|---|---|---|
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 |
Resolve-RelativePath -Path 'C:\Program Files' -FromDirectory 'C:\Windows\system32'
Returns ..\..\Program Files
.
Get-ChildItem * | Resolve-RelativePath -FromDirectory 'C:\Windows\system32'
Returns the relative path from the C:\Windows\system32
directory to the current directory.
Resolve-RelativePath -Path 'C:\I\do\not\exist\either' -FromDirectory 'C:\I\do\not\exist'
Returns .\either
.
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
.