Removing elements and attributes

Elements may be removed from a document by selecting the node, then calling the RemoveChild method on the parent:

[Xml]$xml = @" 
<?xml version="1.0"?> 
<list type='numbers'> 
<name>1</name> 
<name>2</name> 
<name>3</name> 
</list> 
"@ 
$node = $xml.SelectSingleNode('/list/*[.="3"]') 
$null = $node.ParentNode.RemoveChild($node) 

The RemoveAll method is also available; however, this removes all children (and attributes) of the selected node.

Attributes are similarly easy to remove from a document:

$xml.list.RemoveAttribute('type') 

Get Mastering Windows PowerShell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.