How to update the specific node of the XML file using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

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.

Published on 01-Mar-2021 10:01:29