How to convert absolute path to relative path in PowerShell?

Using the built-in System.IO.Path.GetRelativePath is simpler than the accepted answer:

[System.IO.Path]::GetRelativePath($relativeTo, $path)

I found something built in, Resolve-Path:

Resolve-Path -Relative

This returns the path relative to the current location. A simple usage:

$root = "C:\Users\Dave\"
$current = "C:\Users\Dave\Documents\"
$tmp = Get-Location
Set-Location $root
Resolve-Path -relative $current
Set-Location $tmp

Tags:

Powershell