Update Specific Node of XML File Using PowerShell



To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method.

We have below the XML file from the link stored in SampleXml.XML on C:\Temp location.

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

In this example, we are going to update Autor and Genre properties of the Book having attribute Id = ‘bk102’

$xml=[xml](Get-Content C:\Temp\SampleXML.xml)


$node=$xml.SelectSingleNode("//book[@id='bk102']")

The above commands will load the XML file and select node with attribute value ‘bk102’.

$node.genre='Non-Fiction'
$node.author='Dell James'
$xml.Save("C:\Temp\SampleXML.xml")

The above commands will update the genre and author property.

Updated on: 2021-03-01T12:09:27+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements