Updating XML Content Using XPath

by Mar 25, 2016

You can use Select-Xml to select content from an XML file, and then change or update its values. This is very powerful. You just need to get familiar with the XPath query language.

Before you play: In a previous tip we showed how to create a new XML file from scratch. This XML document is used in the next example. If you missed the tip, you find the content of the XML file below. Make sure you save it into $env:temp\inventory.xml.

Let's see how you can assign a new GUID for "Server0009":

# this is where the XML sample file was saved
$Path = "$env:temp\inventory.xml"
 
# load it into an XML object
$xml = New-Object -TypeName XML

$xml.Load($Path)

$item = Select-Xml -Xml $xml -XPath '//Machine[Name="Server0009"]'
$computer = $item.Node 
$computer.GUID = [Guid]::NewGuid().Guid.ToString()

$xml.Save($Path)

Here is the sample XML document:

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<!–List of machines–>
<Machines current="True" manager="Tobias">
          <!–1. machine details–>
          <Machine test="940854388">
                   <Name>Server0001</Name>
                   <IP>40.101.105.27</IP>
                   <GUID>8c49e799-b661-4076-b325-f030d25b299e</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–2. machine details–>
          <Machine test="1567577618">
                   <Name>Server0002</Name>
                   <IP>161.29.237.75</IP>
                   <GUID>eca864a0-28a6-4e60-9438-c58b3f4c5b91</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–3. machine details–>
          <Machine test="1385488736">
                   <Name>Server0003</Name>
                   <IP>202.222.193.169</IP>
                   <GUID>487b0b85-d03b-4fe1-bec1-895bb4c6f43d</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–4. machine details–>
          <Machine test="1141441915">
                   <Name>Server0004</Name>
                   <IP>115.249.246.167</IP>
                   <GUID>917a1243-c354-41d8-aa8e-5b9bf0334a80</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–5. machine details–>
          <Machine test="2134573729">
                   <Name>Server0005</Name>
                   <IP>239.97.156.231</IP>
                   <GUID>f961916d-ccfb-4395-8e61-59299ef11926</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–6. machine details–>
          <Machine test="1569812933">
                   <Name>Server0006</Name>
                   <IP>66.5.84.251</IP>
                   <GUID>154e5e92-f063-4571-8987-2d3e76b34331</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–7. machine details–>
          <Machine test="426025640">
                   <Name>Server0007</Name>
                   <IP>99.140.184.148</IP>
                   <GUID>0568c635-8bd9-4834-8084-28e1fd78d512</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–8. machine details–>
          <Machine test="1706184427">
                   <Name>Server0008</Name>
                   <IP>129.242.172.7</IP>
                   <GUID>b106eff3-5642-4857-a12d-53d6f552bf07</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–9. machine details–>
          <Machine test="1724935941">
                   <Name>Server0009</Name>
                   <IP>165.234.115.96</IP>
                   <GUID>e72b07ae-e5fa-49a9-a663-f17b4894c50d</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–10. machine details–>
          <Machine test="154244134">
                   <Name>Server0010</Name>
                   <IP>191.162.109.0</IP>
                   <GUID>f4568d8d-2302-404c-8c0e-1f7047cf8a1a</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
</Machines>

Twitter This Tip! ReTweet this Tip!