-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
.NET uses the current process' current directory (working directory) to resolve filesystem paths, as reflected in [Environment]::CurrentDirectory
Every PowerShell runspace maintains its own current directory (location) that can deviate from the process's, which can have surprising results when you pass relative paths to .NET methods and constructors.
Here's a simple example:
# Assume that PowerShell's location is the current user's home directory,
# e.g., /Users/jdoe
PS> Set-Location /; [io.fileinfo]::new('foo').fullname
/Users/jdoe/foo # !! Resolved relative to /Users/jdoe, not /
This pitfall should be documented in about_Methods
and about_Locations
.
-
contains a discussion of why, unfortunately, always keeping the two locations in sync is not an option
-
offers workarounds.
PowerShell/PowerShell#7197 suggests enhancing the Convert-Path
cmdlet to make it easier to resolve relative paths and paths based on custom PS drives to full filesystem paths so that they can be safely passed to .NET methods.
Issue Details
Version(s) of document impacted
- Impacts 6.1 document
- Impacts 6.0 document
- Impacts 5.1 document
- Impacts 5.0 document
- Impacts 4.0 document
- Impacts 3.0 document