0

I have a function:

function foo {
    param (
        [Parameter(ValueFromPipeline = $true)]
        [System.IO.DirectoryInfo[]]$path
    )
    foreach ($p in $path) {
        Write-Host $p.FullName
    }
}

where '.' refers to the script's home directory instead of current directory.

PS D:\> foo .,"$pwd"      
D:\ps1
D:\

I want foo . returns D:\ instead of D:\ps1, where the current directory is.

Z2AKxI
  • 31
  • 2
  • [`pwd`](https://stackoverflow.com/q/21846161/1701026) refers to the current path, [`PSScriptRoot`](https://stackoverflow.com/q/5466329/1701026) refer to "*the script's home directory*". – iRon Dec 03 '22 at 08:46
  • See also: [about automatic variables](https://learn.microsoft.com//powershell/module/microsoft.powershell.core/about/about_automatic_variables) – iRon Dec 03 '22 at 09:26
  • @iRon Maybe my original question isn't clear enough, I want `foo .` returns current directory. But it in fact returns `PSScriptRoot`. – Z2AKxI Dec 03 '22 at 11:32
  • In my case, I get indeed the same results for `.` and `"$pwd" `. Anyways, I have reopened the issue. – iRon Dec 03 '22 at 12:14
  • @iRon I also found that `[System.IO.DirectoryInfo]'.'` will return the path where pwsh is opened, so maybe that's why. Perhaps I need to use `[string]` instead then replace with `$pwd`. – Z2AKxI Dec 03 '22 at 12:23
  • It's .Net `[System.IO.Directory]::GetCurrentDirectory()` because you're using `IO.FileInfo` to resolve a relative path – Santiago Squarzon Dec 03 '22 at 13:14

0 Answers0