The problem
You have an absolute path, but you want it to be relative to another path.
Example:
P:/SO/data/database.txt
--> Now we want the filename to be relative to: P:/SO/team/lists/
../../data/database.txt
I have already found the Stack Overflow
question How to convert absolute path to relative path in PowerShell?.
One answer links to an already developed Cmdlet, but this one didn’t work for me. And the trick using Set/Get-Location requires the paths to exist.
The solution
I found an answer from Gordon written in PHP: Getting relative path from absolute path in PHP.
Here is my port to PowerShell:
Attention:
– You From path has to end with a slash!
Example