Saving an XML file in powershell requires complete path. Why? Saving an XML file in powershell requires complete path. Why? xml xml

Saving an XML file in powershell requires complete path. Why?


The reason is because the current working directory in powershell isn't necessarly the same as the process working directory.

This is because the powershell working direction ( Get-Location ) can make use of the different filesystem providers - such as the Registry or Certificates.Also because powershell can have multiple runspaces, each one can have their own current working directory.

There are two workarounds to this.

One is to use Resolve-Path (Resolve-Path "orders.xml"), although this will check existence prior to returning the value, so for creation of new files, this may be an issue.The other option is to use the alias of get-location : $pwdfor instance ("$pwd\orders.xml")

So for your example you could either change line 2 to be

$config = [xml](get-content (Resolve-Path "web.config"))

or

$config = [xml](get-content "$pwd\web.config")

and respectivlyline 12 to be

$config.Save((Resolve-Path "web.config"));

or

$config.Save("$pwd\web.config");


If it didn't go to the current working directory, I'd check $home.