If you need to make changes to an existing XML document, for example to update inventory data, the easiest way is to load the document into an XML object. You can then use default PowerShell cmdlets to find the node you want to change, and change it. All changes can then easily be saved to the same or a new XML document. You do not have to worry at all about the XML object model or its syntax.
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 change the IP address for "Server0005":
# 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) # note: if your XML is malformed, you will get an exception here # always make sure your node names do not contain spaces # simply traverse the nodes and select the information you want $Xml.Machines.Machine | Where-Object Name -eq Server0005 | ForEach-Object { $_.IP = '10.10.10.10' } $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>