DevOps | Scripts | Automation

PowershellXML

How to update, add or delete XML nodes using PowerShell?

In previous articles, I have written some content on how to create an XML file using PowerShell and how to use the xPath using PowerShell. You can refer to them below.

Create XML File using PowerShell

How to use xPath in XML?

Use xPath in XML using PowerShell – Part 2

This article has below points covered.

  • Update Node Value
  • Add a new node and attribute in XML
  • Delete specific Node in XML file

Consider the book XML sample from the Microsoft site below.

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)

Update Node value

Updating the values of the node using xPath is super simple. We just need to overwrite the value of the node. For example, we need to update the book ID “bk102” value author node. The below command will get the current value of the Author node.

$xmlFile = "C:\temp\books.xml"
$booksXml = [xml](gc $xmlFile)
$booksXml.SelectSingleNode("//book[@id='bk102']").author
Books Author value

To update this value simply assign the value to the about author and it will replace the current value but of course, you need to save the file again.

$xmlFile = "C:\temp\books.xml"
$booksXml = [xml](gc $xmlFile)
$booksXml.SelectSingleNode("//book[@id='bk102']").author = 'Chirag Nagrekar'
$booksXml.Save($xmlFile)

Once you check the file, that node value will be updated.

Add a new node and attribute in XML

To add the new node and its attributes in the XML file with PowerShell, you must create an element first.

The below command will create a new element name “book” and add it to the end of the last node.

$xmlFile = "C:\temp\books.xml"
$booksXml = [xml](gc $xmlFile)
$newbookElement = $booksXml.CreateElement("book")
$newbookElementAdd = $booksXml.catalog.AppendChild($newbookElement)


In the above command note that we are adding a child to the catalog node (parent node).

To set its attribute value, id=bk113, use the below command.

$newattribute = $newbookElementAdd.SetAttribute("id","bk113")
$booksXml.Save($xmlFile)

Once you write the above code, the new node will be added. The output will be as below.

New Node

A new node is added, you need to add child nodes to it and add some attributes. Let’s see the code below for it.

# add Author child node with description
$authorElement = $booksXml.CreateElement("author")
$authorElementAdd = $bookNode.AppendChild($authorElement)
$authorElementAdd.InnerText = "Chirag Nagrekar"


# add title child elements with description and attribute.
$titleElement = $booksXml.CreateElement("title")
$titleElementAdd = $bookNode.AppendChild($titleElement)
$titleElementAdd.InnerText = "PowerShell Scripting"
$titleElement.SetAttribute("published","true")

The first child node ‘Author‘ will be added to the parent node bk113 parent node and the Title child node will be also appended to the same bk113 node with the attribute “published“.

The entire code will be as below,

$xmlFile = "C:\temp\books.xml"
$booksXml = [xml](gc $xmlFile)
$newbookElement = $booksXml.CreateElement("book")
$newbookElementAdd = $booksXml.catalog.AppendChild($newbookElement)

$newattribute = $newbookElementAdd.SetAttribute("id","bk113")

$bookNode = $booksXml.SelectSingleNode("//book[@id='bk113']")

# add Author child node with description
$authorElement = $booksXml.CreateElement("author")
$authorElementAdd = $bookNode.AppendChild($authorElement)
$authorElementAdd.InnerText = "Chirag Nagrekar"


# add title child elements with description and attribute.
$titleElement = $booksXml.CreateElement("title")
$titleElementAdd = $bookNode.AppendChild($titleElement)
$titleElementAdd.InnerText = "PowerShell Scripting"
$titleElement.SetAttribute("published","true")

$booksXml.Save($xmlFile)

Output:

Output of Added node

Delete specific Node in XML file

The below commands will delete the bk113 node from the XML file.

$xmlFile = "C:\temp\books.xml"
$booksXml = [xml](gc $xmlFile)
$node =  $booksXml.SelectSingleNode("//book[@id='bk113']")
$node.ParentNode.RemoveChild($node)
$booksXml.Save($xmlFile)

Let’s say you need to delete title child node for the specific book id bk113 for that you need xPath query to it as shown below.

$node =  $booksXml.SelectSingleNode("//book[@id='bk113']/title")