Get relative path of files in sub-folders from the current directory Get relative path of files in sub-folders from the current directory powershell powershell

Get relative path of files in sub-folders from the current directory


The Resolve-Path cmdlet has a -Relative parameter that will return a path relative to the current directory:

Set-Location C:\MyScript$relativePath = Get-Item Data\Test.txt | Resolve-Path -Relative


In case the directory doesn't exist, or the base directory should be different from the current working directory, there's a .NET method [IO.Path]::GetRelativePath(String from, String to).