March 30th, 2023
heart12 reactions

Update XML files using PowerShell

Sonam Rastogi
Senior Service Engineer

There are many available blog posts on internet explaining how to updateXML files inPowerShell. But I felt need of one consolidated blog where complex XML files can be updated using PowerShell. These complex xml files can have long complex hierarchy of XML nodes and attributes.

Let us try to update below XML sample at various levels of node hierarchy.

Sample XML Code

<?xml version="1.0" encoding="utf-8"?><Data version="2.0">  <Roles>    <Role Name="ManagementServer" Value="OldManagementServer" />  </Roles>  <SQL>    <Instance Server="OldSQLServer" Instance="MSSQLSERVER" Version="SQL Server 2012">      <Variable Name="SQLAdmin" Value="Domain\OldSQlAdmin" />      <Variable Name="SQLUser" Value="domain\sqluser" />    </Instance>  </SQL>  <VMs>    <VM Type="ClientVM">      <VMName>ClientVM</VMName>    </VM>    <VM Type="DNSServerVM">      <VMName>OldDNSServer</VMName>    </VM>  </VMs></Data>

Image original

Steps to follow

Save the above xml block in C: drive with name “Data.xml”.

We will update the nodes in XML file to use a new management, SQL, and DNS servers. Below are the step-by-step PowerShell commands on how we can update the nodes and their attributes at various levels.

  1. Define the variables that we need to modify.

    $path             = 'C:\Users\sorastog\Desktop\blog\Variable.xml'$ManagementServer = 'NewManagementServer'$SQLServer        = 'NewSQLServer'$SQLAdmin         = 'Domain\NewSQlAdmin'$DNSServerVMName  = 'NewDNSServer'
  2. Read the content of XML file.

    $xml = [xml](Get-Content -Path $path)
  3. UpdateManagementServer: Change the attributeValue of nodes at level 3 based on theName attribute on the same level.

    $node = $xml.Data.Roles.Role | where {$_.Name -eq 'ManagementServer'}$node.Value = $ManagementServer
  4. UpdateSQLServer: Change the attributeValue of a node at level 3.

    $node        = $xml.Data.SQL.Instance$node.Server = $SQLServer
  5. UpdateSQLAdmin: Change the attributeValue of nodes at level 4 based on theName attribute on the same level.

    $node = $xml.Data.SQL.Instance.Variable |where {$_.Name -eq 'SQLAdmin'}$node.Value = $SQLAdmin
  6. UpdateDNSServerVM: Change the attributeValue of nodes at level 4 based on theVMType attribute at the level above.

    $node = $xml.Data.VMs.VM |where {$_.Type -eq 'DNSServerVM'}$node.VMName = $DNSServerVMName
  7. Save changes to the XML file.

    $xml.Save($path)

Output

The final PowerShell script will look like below:

$path             = 'C:\Data.xml'$ManagementServer = 'NewManagementServer'$SQLServer        = 'NewSQLServer'$SQLAdmin         = 'Domain\NewSQlAdmin'$DNSServerVMName  = 'NewDNSServer'$xml = [xml](Get-Content $path)$node = $xml.Data.Roles.Role |    where {$_.Name -eq 'ManagementServer'}$node.Value = $ManagementServer$node        = $xml.Data.SQL.Instance$node.Server = $SQLServer$node = $xml.Data.SQL.Instance.Variable |    where {$_.Name -eq 'SQLAdmin'}$node.Value = $SQLAdmin$node = $xml.Data.VMs.VM |    where {$_.Type -eq 'DNSServerVM'}$node.VMName = $DNSServerVMName$xml.Save($path)

The final xml will look like below.

Image updated

Hope this post will help you to update complex XML files using PowerShell. If there are suggestions on how to improve this blog post, then please comment below. I will be happy to include them.

Till Then, Happy Scripting 🙂

Follow more PowerShell postshere.

Author

Sonam Rastogi
Senior Service Engineer

I have 12+ years of development experience in C#, PowerShell, SQL, Azure, PowerBI and other Microsoft technologies.

4 comments

Discussion is closed.Login to edit/delete existing comments.

Stay informed

Get notified when new posts are published.
Follow this blog
facebook