Josh Buedel
Josh Buedel

Reputation: 1863

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?

Upvotes: 19

Views: 3833

Answers (2)

Shay Levy
Shay Levy

Reputation: 126722

[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

Upvotes: 21

zdan
zdan

Reputation: 29450

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

Upvotes: 13

Related Questions