Powershell: resolve path that might not exist?

powershell

Solution

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

Problem

I'm trying to process a list of files that may or may not be up to date and may or may not yet exist. In doing so, I need to resolve the full path of an item, even though the item may be specified with relative paths. However, `Resolve-Path` prints an error when used with a non-existant file. For example, What's the simplest, cleanest way to resolve `".\newdir\newfile.txt"` to `"C:\Current\Working\Directory\newdir\newfile.txt"` in Powershell? Note that `System.IO.Path`'s static method use with the process's working directory - which isn't the powershell current location.

Original source