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.
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?
[System.IO.Path] is using the shell process’ current directory. You can get the absolute path with the
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
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Ebook Reviews
- PES Matches, Skills & News