0

Is there any way I can update my XML file using PowerShell:

My Current file format:

   <?xml version="1.0"?>
   <Configuration>
     <SPaths>
        <path>Share</path>
     </SPaths>

My output should be:

   <?xml version="1.0"?>
   <Configuration>
     <SPaths>
        <path>My Share</path>
        <path>My Share/xyz</path>
        <path>My Share/abc</path>
     </SPaths>

I am using the below commands

   $myXML = [xml](Get-Content 'C:\MyPath.config')
   $myXML.Configuration.SPaths.path = "My Share"
   $myXML.Save("C:\MyPath.config")

I need to add 2 more lines of code, Any Help much appreciated, Thank you.

Hussain
  • 49
  • 2
  • 10

1 Answers1

0

with the below-given commands, I am able to add new lines to my XML file. Thanks Daniel

$myXML.Configuration.SPaths.path = "My Share"
$xmlElt = $myXML.CreateElement("path")
$xmlText = $myXML.CreateTextNode("My Share/xyz")
$xmlElt.AppendChild($xmlText)
$myXML.Configuration.SPaths.InsertBefore($xmlElt, $myXML.Configuration.SPaths.legacyUnhandledExceptionPolicy)
$xmlElt1 = $myXML.CreateElement("path")
$xmlText1 = $myXML.CreateTextNode("My Share/abc")
$xmlElt1.AppendChild($xmlText1)
$myXML.Configuration.SPaths.InsertBefore($xmlElt1, $myXML.Configuration.SPaths.legacyUnhandledExceptionPolicy)
$myXML.Save("C:\MyPath.config")```
Hussain
  • 49
  • 2
  • 10