How to Modify XML File Content Using PowerShell Script
The XML
, also known as Extensible Markup Language
, is mainly used for representing structured information like documents, data, books, etc., using a simple text-based format.
This article demonstrates how we can modify an XML
file using the PowerShell Script. Also, we will see necessary examples and explanations to make the topic easy to understand.
Suppose we have an XML
file with the below content.
<settings>
<setting id="LocationOne" value="Dhaka, Bangladesh" />
<setting id="LocationTwo" value="zmw:00000.1.08181" />
</settings>
Now we are going to modify the above XML
file by using three methods.
Method 1: Use the child position
If you know the position of the child of the specific parent, you can easily modify the value of the particular child node. For this purpose, we can follow the code example given below.
$xml = [xml](Get-Content "G:\PowerShell\Myxml.xml")
$xml.settings.ChildNodes.Item(0).value = "New York, USA"
$xml.Save("G:\PowerShell\Myxml.xml")
In our above example, we first read the content of the XML file and then find the child node by using its position. Here the ChildNodes
are used to find the child node, and we pick the specific child using Item(0)
.
Lastly we save the modified XML
file using the line $xml.Save("G:\PowerShell\Myxml.xml")
. After executing the script, you will see the following content inside your XML
file.
<settings>
<setting id="LocationOne" value="New York, USA" />
<setting id="LocationTwo" value="zmw:00000.1.08181" />
</settings>
Method 1: Use the ID
We also can modify the XML
file by using the id
of a specific element. Here the id
is mainly used to pick the particular child node. You can use this method to modify the XML file by following the example shared below:
$xml = [xml](Get-Content "G:\PowerShell\Myxml.xml")
$xml.settings.SelectSingleNode("//setting[@id='LocationOne']").value = "New York, USA"
$xml.Save("G:\PowerShell\Myxml.xml")
Just like our previous example, we first read the contents of the XML file. After that we identify an pick the specific child node by using SelectSingleNode("//setting[@id='LocationOne']")
and change its value by using value
.
Lastly we saved the modified XML
file using the line $xml.Save("G:\PowerShell\Myxml.xml")
,
now after executing the above script, you will see the below content inside your XML
file.
<settings>
<setting id="LocationOne" value="New York, USA" />
<setting id="LocationTwo" value="zmw:00000.1.08181" />
</settings>
Method 3: Use settings.skinsettings.setting
You can follow this example if a parent node contains a single child node. To understand this method suppose we have an XML
file with the below content.
<settings>
<skinsettings>
<setting type="bool">false</setting>
</skinsettings>
</settings>
Look at our example below, where we modified the XML
file shared above using this method. The code for our example will be as follows:
$xml = [xml](Get-Content "G:\PowerShell\Myxml.xml")
$xml.settings.skinsettings.setting."#text" = "true"
$xml.Save("G:\PowerShell\Myxml.xml")
In the example above, after reading the content of the XML
file, we pick the specific child node by using the line settings.skinsettings.setting
.
Lastly we saved the modified XML
file using the line $xml.Save("G:\PowerShell\Myxml.xml")
. You can use this method to modify the XML
file by following the example shared below:
<settings>
<skinsettings>
<setting type="bool">true</setting>
</skinsettings>
</settings>
Please note that the example codes shared here are only executable on the Windows PowerShell environment.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn