You can use a combination of pwd , Join-Path and [System.IO.Path]::GetFullPath to get a fully extended path.
Since cd ( Set-Location ) does not change the working directory of the current process, simply passing the relative file name to the .NET API, which the PowerShell context does not understand, can have unintended side effects, such as allowing a path based on the original working directory (rather than at your current location).
What you do is that you first determined your path:
Join-Path (Join-Path (pwd) fred\frog) '..\frag'
This gives (given my current location):
C:\WINDOWS\system32\fred\frog\..\frag
With an absolute base, you can safely call the .NET API GetFullPath :
[System.IO.Path]::GetFullPath((Join-Path (Join-Path (pwd) fred\frog) '..\frag'))
Which gives you the full path and with the removal .. :
C:\WINDOWS\system32\fred\frag
This is also not difficult, I personally despise solutions that depend on external scripts for this, this is a simple problem that can be solved quite accurately with Join-Path and pwd ( GetFullPath just to make it pretty). If you want to keep only the relative part, you simply add .Substring((pwd).Path.Trim('\').Length + 1) and voila!
fred\frag
UPDATE
Thanks to @Dangph for specifying the case of the edge of C:\ .
John Leidegren Dec 12 '12 at 19:39 2012-12-12 19:39
source share