Search code examples
xmlpowershellpowershell-4.0

PowerShell outputs XML data on the end of execution


When I parse an xml file by reading it with Get-Content and casting it to XML and then insert a new node into it, PowerShell echoes the content of the newly created node. Why is PowerShell doing that?

$proj = [xml](Get-Content $project.AbsolutePath)

$element = $proj.CreateElement("Version");
$element.InnerText = "1.0.0";
$proj.Project.PropertyGroup.AppendChild($element);

Write-Host "OK"

After that there is really no other code than that. But it still echoes the following text:

OK
#text
-----
1.0.0

I have tried to set the variable $element to $null, just in case there is some weird global variable I should not touch. I even told the script to exit on the end of the script. When I bypass the execution of the insertion of the new node, no additional text is written to the end of the script.


Solution

  • AppendChild() returns the new node, which PowerShell is displaying as it doesn't know what else to do with it. Something like this will prevent that:

    $proj.Project.PropertyGroup.AppendChild($element) | Out-Null