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

blue18hutthutt picture blue18hutthutt · Jun 10, 2012 · Viewed 47.6k times · Source

Is there any straight-forward way (by use of cmdlets or .NET classes) to obtain only the relative path of a file in a sub-folder from a given path?

eg current folder is C:\MyScript and there is a sub-folder called Data with a file Test.txt, so I would like to see Data\Test.txt instead of C:\MyScript\Data\Test.txt

Answer

Aaron Jensen picture Aaron Jensen · Jun 11, 2012

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