Current Working Directory with PowerShell and .Net Calls

Mon, Jun 26, 2006 One-minute read

A question that occasionally comes up is, “Why doesn’t PowerShell change the [Environment]::CurrentDirectory while I navigate around the shell?”  This is always in the context of using a relative file path while working with the .Net framework.

One of the difficult aspects of this comes from the fact that PowerShell supports multiple pipelines of execution.  Although it’s not directly exposed yet, users will soon be able to suspend jobs to the background, and other concurrent tasks.

The current directory affects the entire process, so if we change the directory as you navigate around the shell, you risk corrupting the environment of jobs you have running in the background.

When you use filenames in .Net methods, the best practice is to use fully-qualified path names.  The Resolve-Path cmdlet makes this easy:

PS:37 > $reader = new-object System.Xml.XmlTextReader baseline.xml

PS:38 > $reader.BaseURI
file:///C:/Documents and Settings//baseline.xml

PS:37 > $reader = new-object System.Xml.XmlTextReader (resolve-path baseline.xml)

PS:38 > $reader.BaseURIfile:///C:/temp/baseline.xml