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?
[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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With