Powershell: resolve path that might not exist?

You want:

c:\path\exists\> $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\nonexist\foo.txt")

returns:

c:\path\exists\nonexists\foo.txt

This has the advantage of working with PSPaths, not native filesystem paths. A PSPath may not map 1-1 to a filesystem path, for example if you mount a psdrive with a multi-letter drive name.

What’s a pspath?

ps c:\> new-psdrive temp filesystem c:\temp
...
ps c:\> cd temp:
ps temp:\> 

temp:\ is a drive-qualified pspath that maps to a win32 (native) path of c:\temp.

-Oisin

Leave a Comment