Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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?

like image 659
Josh Buedel Avatar asked Nov 01 '10 18:11

Josh Buedel


2 Answers

[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
like image 88
Shay Levy Avatar answered Oct 20 '22 04:10

Shay Levy


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
like image 27
zdan Avatar answered Oct 20 '22 05:10

zdan