Using PowerShell to add new child node in element

By | March 8, 2016

Code template:

$path = '<path to xml file>'

 $xml = Get-Content($path)

$newNode = $xml.CreateElement('<name of new element>')
$newNode.SetAttribute('<Attribute key>','<Attribute value>')
$xml.ParentElement.AppendChild($newNode)
$xml.Save($path)

‘ParentElement’ is the name of the element which you want to add the new child to.
Always remember to save the file or it will not be updated.

Leave a Reply