

$newXmlNameTextNode = $newXmlNameElement.AppendChild($xmlDoc.CreateTextNode("Iain Brighton")) To add the text node we can use the following code: $newXmlNameElement = $newXmlEmployee.AppendChild($xmlDoc.CreateElement("name")) This is probably the bit that took longest to work out. Iain Brighton, this is known as a text node (at least in XDocument speak). Unbeknownst to me, when you have text within an element tag, i.e. $newXmlAgeElement = $newXmlEmployee.AppendChild($xmlDoc.CreateElement("age"))
#POWERSHELL XML TOOLS HOW TO#
Knowing how to add elements we can simply create our sub elements and attach them to our newly created parent reference: $newXmlNameElement = $newXmlEmployee.AppendChild($xmlDoc.CreateElement("name")) Now our resulting XML file looks like this: We can create an XmlDocument attribute on our element with the following code: $newXmlEmployee = $($xmlDoc.CreateElement("employee")) To add the ID attribute tag in the XML document, we need to create a new XmlDocument Attribute and then attach it to our newly created element. If we examine the resulting XML file we’ll find the following (note the new empty element): To shorten this we can just use this (see here for more information on the CreateElement method): $newXmlEmployee = $($xmlDoc.CreateElement("employee")) $newXmlEmployee = $($newXmlEmployeeElement) Now we can create our new element/node, append it to the parent reference and save it: $newXmlEmployeeElement = $xmlDoc.CreateElement("employee") Firstly we need to load the XML document: $fileName = “employees.xml” If we wish to add a new employee record to the above document, we need to create a new XML element (or node). Apologies the XML formatting is stripped by the plugin:

Here is the example we’ll use in this post. Net object here but it is possible to utilise other. Reading XML files is one thing, but as the previous article mentions, inserting (multiple) elements can be quite convoluted. As a starting point you might want to read up here first.
#POWERSHELL XML TOOLS TRIAL#
There are lots of forum posts out there that detail snippets of information, but a lot of it is trial and error. Hopefully this post will lay the basis for some more App-V 5.0 focused blogs in the future. The only real option is to utilise the built-in. I have seen a lot of “rip and replace” and “find and replace” script examples, but ensuring that the correct elements/nodes are added in the correct place can be troublesome. Whilst we can manually tweak them, there’s nothing like automating things for consistency and speed! Microsoft App-V 5.0 and RES Workspace Manager utilise XML files extensively. Manipulating XML files with PowerShell is something that we’re having to accomplish more and more internally.
