How do I alter XML with PowerShell/XPath and save the document?


I'm looking to use PowerShell to alter XML. I haven't been able to copy XML using XPath. I can load the XML, but I can't figure out how to list the XML with an XPath and create another XML file with what I retrieve.

$doc = new-object "System.Xml.XmlDocument"

Also, how would you add the removed XML to another XML file?

By : Bruce227


Load Linq Xml assemblies:


Load your xml (Note, you can use ::Load("file") instead of ::Parse(...) to load from file:

$xml = [System.Xml.Linq.XDocument]::Parse("<root> <row>Hey</row> <row>you</row> </root>")

Modify (in this case Remove the first row:

[System.Xml.XPath.Extensions]::XPathSelectElement($xml, "//row").Remove()

Save to file:


Using System.Xml (instead of System.Xml.Linq):

$doc = new-object "System.Xml.XmlDocument"

$node = $doc.SelectSingleNode("//row");

By : Nestor

This video can help you solving your question :)
By: admin