Why is PowerShell resolving paths from $home instead of the current directory?

Why is PowerShell resolving paths from $home instead of the current directory?

I expect this little powershell one liner to echo a full path to foo.txt, where the directory is my current directory.
[System.IO.Path]::GetFullPath(“.\foo.txt”)

But it’s not. It prints…
C:\Documents and Settings\Administrator\foo.txt

I am not in the $home directory. Why is it resolving there?

Solutions/Answers:

Solution 1:

[System.IO.Path] is using the shell process’ current directory. You can get the absolute path with the Resolve-Path cmdlet:

Resolve-Path .\foo.txt

Solution 2:

According to the documentation for GetFullPath, it uses the current working directory to resolve the absolute path. The powershell current working directory is not the same as the current location:

PS C:\> [System.IO.Directory]::GetCurrentDirectory()
C:\Documents and Settings\user
PS C:\> get-location

Path
----
C:\

I suppose you could use SetCurrentDirectory to get them to match:

PS C:\> [System.IO.Directory]::SetCurrentDirectory($(get-location))
PS C:\> [System.IO.Path]::GetFullPath(".\foo.txt")
C:\foo.txt

References

Loading...