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.

1

1 Answer 1

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")```
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.