You can do this using the CreateElement and AppendChild
Get-ChildItem c:\temp\ *.xml | % { $xml = [xml](Get-Content $_.fullname) $lastName = $xml.CreateElement('LastName') $lastName.PsBase.InnerText = 'SomeName' $null = $xml.People.Names[0].AppendChild($lastName) $xml.Save($_.FullName) }
If you run PowerShell V2, you do not need to use the PsBase property:
$lastName.InnerText = 'SomeName'
There are, of course, other ways, but this one is pretty simple.
In case node is deeper in xml, you can use Xpath like this (both find Names node first):
$node = (Select-Xml -Xml $x -XPath '//Names[1]').Node $node = (Select-Xml -Xml $x -XPath '//Names[position()=1]').Node
source share