2

I'm creating a PowerShell script. It takes a single path as a parameter. I want to manipulate that path and use it in another command, so I need the full path.

I do not want to change the current directory. I just want the full path of the given file.

I have worked with wonderful Java libraries such as Picocli, and I had assumed that merely setting the argument type to System.IO.FileInfo would make PowerShell figure out how where the path is (i.e. relative to the command or absolute). But apparently it isn't that smart, and System.IO.FileInfo casts the string to a file info and assumes that the parent directory for relative paths is the home directory. For example:

[CmdletBinding()]
Param([Parameter(Mandatory, HelpMessage="Archive Path")] [System.IO.FileInfo]$path)
…
Write-Output $path.FullName

I put this script in C:\bin\extract-zip.ps1 and make sure C:\bin\ is on the PATH. Then I go to the C:\temp\ directory and run this command:

extract-foo foo.zip

It prints:

C:\Users\user\foo.zip

This makes absolutely zero sense to me. I expected C:\temp\foo.zip.

So I guess I'll have to manually convert $path to an absolute path. If I know ahead of time that the user provided a relative path, I can do something like this (but I guess I would have to use type String instead of System.IO.FileInfo):

$path = Join-Path ([System.IO.Directory]::GetCurrentDirectory()) $path

But what if the user provided an absolute path? Isn't there some resolve() method that Do The Right Thing if the second argument is absolute? Or is there a method for detecting whether a path is absolute?

Here is the question: I want to convert a given path argument to an absolute path, whether the given path is absolute or relative, without changing the current directory.

I can find no Stack Overflow question that provides a direct answer. For System.IO.FileInfo and Relative Paths someone said to try [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($pwd, '.\test.txt')), but nobody said if this works, and whether it works with both relative and absolute paths.

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • 1
    `Set-Location` and `Push-Location` do not update .NET current directory. See https://stackoverflow.com/questions/70254096/system-io-fileinfo-and-relative-paths – Santiago Squarzon Dec 24 '22 at 16:02

0 Answers0