< Back

Resolve-CRelativePath

Sun Jan 12, 2020 11:01 pm

NAME Resolve-CRelativePath



SYNOPSIS

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





SYNTAX

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



Resolve-CRelativePath [-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](http://msdn.microsoft.com/en-us/library ... 73740.aspx).



Neither the `From` or `To` paths need to exist.





PARAMETERS

-Path <String>

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



Required? false

Position? named

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-FromDirectory <String>

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

object.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-FromFile <String>

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

object.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Resolve-CRelativePath -Path 'C:\\Program Files' -FromDirectory 'C:\\Windows\\system32'



Returns `..\\..\\Program Files`.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Get-ChildItem * | Resolve-CRelativePath -FromDirectory 'C:\\Windows\\system32'



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









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Resolve-CRelativePath -Path 'C:\\I\\do\\not\\exist\\either' -FromDirectory 'C:\\I\\do\\not\\exist'



Returns `.\\either`.









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Resolve-CRelativePath -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`.











RELATED LINKS

http://msdn.microsoft.com/en-us/library ... 73740.aspx