XML in Powershell - beginning

XML in Powershell for beginners :)

I have the following XML structure:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<roles version="2.0">
	<role id="7722" name="System1">
		<system permission="allow"/>
		<folders permission="allow"/>
		<globalfavorites/>
		<user>joe</user>
	</role>
</roles>

Open in new window


(read XML)

How do I add a role? (including childs)

How do I delete a role? (including childs)

(and save to file)


Thanks in advance

Michael
LVL 1
mikeydkAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Define some xml:
# can as well be done with 
# $xml = [xml](Get-Content -Path C:\Temp\Whatever.xml)
$xml = [xml]@'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<roles version="2.0">
	<role id="7722" name="System1">
		<system permission="allow"/>
		<folders permission="allow"/>
		<globalfavorites/>
		<user>joe</user>
	</role>
</roles>
'@

Open in new window

You add a node by first creating a new element:
$element = $xml.CreateElement('role')

Open in new window

Then you select the node where you want to add the child (using an XPath, https://www.w3schools.com/xml/xpath_intro.asp), and append the new element:
$roles = $xml.SelectSingleNode('roles')
$newRole = $roles.AppendChild($element)

Open in new window

Finally you add the attributes you want the node to have:
$newRole.SetAttribute('id', 42)
$newRole.SetAttribute('name', 'System2')

Open in new window

The children are added just the same; here as one-liners.
$newRole is assumed to be still set to the new node created in the step before:
$system = $newRole.AppendChild($xml.CreateElement('system'))
$system.SetAttribute('permission', 'allow')
$folders = $newRole.AppendChild($xml.CreateElement('folders'))
$folders.SetAttribute('permission', 'allow')
$globalfavorites = $newRole.AppendChild($xml.CreateElement('globalfavorites'))

Open in new window

For the "user" node, you need to set the node's "InnerText":
$user = $newRole.AppendChild($xml.CreateElement('user'))
$user.InnerText = 'joe'

Open in new window


Deleting a node is similar; you select the parent node (assumed to still be set in $roles from the previous command), you select the node to delete (here for example by the "id" attribute), and call the parent's RemoveChild() method:
$role42 = $xml.SelectSingleNode("roles/role[@id='42']")
$deletedRole = $roles.RemoveChild($role42)

Open in new window

To save, you just call the xml object's Save() method:
$xml.Save('C:\Temp\Whatever.ps1')

Open in new window


Note that in XML, node and attribute names are case sensitive, and so are the comparisons in an XPath. There are ways around that in XPath directly, but it can be easier to do case insensitive filtering in PowerShell. For example, if you want to filter by the name attribute, where it could be system1 or System1:
$system1 = $xml.SelectNodes('roles/role') | Where-Object {$_.GetAttribute('name') -eq 'system1'}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.