4

I need Powershell 5.1 to resolve(normalize) existing local file path similar to tcl normalize or POSIX realpath similar to this, this and this.

Say this filename referenced by full path:

D:\temp\multilink\junction-to-other-dir\absolute-symlink-to-other-dir\relative-symlink-to-other-dir\junction_target_dir_contents.txt

should resolve to

D:\exp\junction_target_dir\junction_target_dir_contents.txt

Resolve-Path and [System.IO.Path]::GetFullPath are not resolving symlinks and junctions.

Traversing path up and resolving each folder using (Get-Item $parent_path_part).Target[0] works but looks cumbersome.

Good old GetFinalPathNameByHandle API does work.

Am I missing any modern(simple) Powershell 5+ way to accomplish this?

EDIT: THIS QUESTION IS NOT 100% DUPLICATE OF powershell to resolve junction target path.

First of all I am looking to resolve multiple junctions and/or symlinks that may be present in the given file path.

Secondly I am looking for nice, simple and possible one-line solution given the improvements of Powershell 5 and 5.1.

That is I am looking for solution ALTERNATE to ones listed in linked questions.

Community
  • 1
  • 1
Anton Krouglov
  • 3,077
  • 2
  • 29
  • 50
  • Possible duplicate of [Powershell to resolve junction target path](http://stackoverflow.com/questions/16926127/powershell-to-resolve-junction-target-path) – Jeff Zeitlin Feb 09 '17 at 18:25
  • @Jeff Zeitlin: I have referenced this and two other would be duplicates in my question. Still I am asking about a bit different thing - complete path resolution not just one part of it. – Anton Krouglov Feb 09 '17 at 18:31
  • Possible duplicate of [powershell to resolve junction target path](http://stackoverflow.com/questions/16926127/powershell-to-resolve-junction-target-path) – sodawillow Feb 09 '17 at 20:34
  • Why do you think using Get-Item `target` is cumbersome? Anyway, I don't see any other related usages of FSCTL_GET_REPARSE_POINT in powershell source code on github. – wOxxOm Feb 09 '17 at 21:13
  • 1
    @wOxxOm: It is easy for simple cases like `c:\junction\file.txt`. It is complex for say `c:\junction1\absolute-symlink\relative-symlink\file.txt` – Anton Krouglov Feb 09 '17 at 21:55
  • Indeed. Well, I see no GetFinalPathNameByHandle used in [.NET 4 source code](https://referencesource.microsoft.com/download.html) as well. – wOxxOm Feb 09 '17 at 23:34
  • Further tests have shown that ps5 get-item does not work well for complex cases. If you stack links on each other funny things happen. Say you create hardlink for symlink. Get-item here completely misses hardlink data. – Anton Krouglov Feb 10 '17 at 09:41

0 Answers0