{"id":895,"date":"2023-03-30T09:07:13","date_gmt":"2023-03-30T16:07:13","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=895"},"modified":"2023-04-01T10:02:30","modified_gmt":"2023-04-01T17:02:30","slug":"update-xml-files-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/update-xml-files-using-powershell\/","title":{"rendered":"Update XML files using PowerShell"},"content":{"rendered":"<p>There are many available blog posts on internet explaining how to update <a href=\"https:\/\/www.w3schools.com\/XML\/xml_whatis.asp\">XML<\/a> files in <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows-server\/administration\/windows-commands\/powershell\">PowerShell<\/a>. 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.<\/p>\n<p>Let us try to update below XML sample at various levels of node hierarchy.<\/p>\n<h2>Sample XML Code<\/h2>\n<pre><code class=\"language-xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;Data version=\"2.0\"&gt;\n  &lt;Roles&gt;\n    &lt;Role Name=\"ManagementServer\" Value=\"OldManagementServer\" \/&gt;\n  &lt;\/Roles&gt;\n  &lt;SQL&gt;\n    &lt;Instance Server=\"OldSQLServer\" Instance=\"MSSQLSERVER\" Version=\"SQL Server 2012\"&gt;\n      &lt;Variable Name=\"SQLAdmin\" Value=\"Domain\\OldSQlAdmin\" \/&gt;\n      &lt;Variable Name=\"SQLUser\" Value=\"domain\\sqluser\" \/&gt;\n    &lt;\/Instance&gt;\n  &lt;\/SQL&gt;\n  &lt;VMs&gt;\n    &lt;VM Type=\"ClientVM\"&gt;\n      &lt;VMName&gt;ClientVM&lt;\/VMName&gt;\n    &lt;\/VM&gt;\n    &lt;VM Type=\"DNSServerVM\"&gt;\n      &lt;VMName&gt;OldDNSServer&lt;\/VMName&gt;\n    &lt;\/VM&gt;\n  &lt;\/VMs&gt;\n&lt;\/Data&gt;<\/code><\/pre>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original-300x162.png\" alt=\"Image original\" width=\"300\" height=\"162\" class=\"alignnone size-medium wp-image-906\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original-300x162.png 300w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original-1024x553.png 1024w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original-768x415.png 768w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original-1536x829.png 1536w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/original.png 1989w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Steps to follow<\/h2>\n<p>Save the above xml block in C: drive with name &#8220;Data.xml&#8221;.<\/p>\n<p>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.<\/p>\n<ol>\n<li>\n<p>Define the variables that we need to modify.<\/p>\n<pre><code class=\"language-powershell\">$path             = 'C:\\Users\\sorastog\\Desktop\\blog\\Variable.xml'\n$ManagementServer = 'NewManagementServer'\n$SQLServer        = 'NewSQLServer'\n$SQLAdmin         = 'Domain\\NewSQlAdmin'\n$DNSServerVMName  = 'NewDNSServer'<\/code><\/pre>\n<\/li>\n<li>\n<p>Read the content of XML file.<\/p>\n<pre><code class=\"language-powershell\">$xml = [xml](Get-Content -Path $path)<\/code><\/pre>\n<\/li>\n<li>\n<p>Update <code>ManagementServer<\/code>: Change the attribute <strong>Value<\/strong> of nodes at level 3 based on the <strong>Name<\/strong> attribute on the same level.<\/p>\n<pre><code class=\"language-powershell\">$node = $xml.Data.Roles.Role | \nwhere {$_.Name -eq 'ManagementServer'}\n$node.Value = $ManagementServer<\/code><\/pre>\n<\/li>\n<li>\n<p>Update <code>SQLServer<\/code>: Change the attribute <strong>Value<\/strong> of a node at level 3.<\/p>\n<pre><code class=\"language-powershell\">$node        = $xml.Data.SQL.Instance\n$node.Server = $SQLServer<\/code><\/pre>\n<\/li>\n<li>\n<p>Update <code>SQLAdmin<\/code>: Change the attribute <strong>Value<\/strong> of nodes at level 4 based on the <strong>Name<\/strong> attribute on the same level.<\/p>\n<pre><code class=\"language-powershell\">$node = $xml.Data.SQL.Instance.Variable |\nwhere {$_.Name -eq 'SQLAdmin'}\n$node.Value = $SQLAdmin<\/code><\/pre>\n<\/li>\n<li>\n<p>Update <code>DNSServerVM<\/code>: Change the attribute <strong>Value<\/strong> of nodes at level 4 based on the <strong>VMType<\/strong> attribute at the level above.<\/p>\n<pre><code class=\"language-powershell\">$node = $xml.Data.VMs.VM |\nwhere {$_.Type -eq 'DNSServerVM'}\n$node.VMName = $DNSServerVMName<\/code><\/pre>\n<\/li>\n<li>\n<p>Save changes to the XML file.<\/p>\n<pre><code class=\"language-powershell\">$xml.Save($path)<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>Output<\/h2>\n<p>The final PowerShell script will look like below:<\/p>\n<pre><code class=\"language-powershell\">$path             = 'C:\\Data.xml'\n$ManagementServer = 'NewManagementServer'\n$SQLServer        = 'NewSQLServer'\n$SQLAdmin         = 'Domain\\NewSQlAdmin'\n$DNSServerVMName  = 'NewDNSServer'\n\n$xml = [xml](Get-Content $path)\n\n$node = $xml.Data.Roles.Role |\n    where {$_.Name -eq 'ManagementServer'}\n$node.Value = $ManagementServer\n\n$node        = $xml.Data.SQL.Instance\n$node.Server = $SQLServer\n\n$node = $xml.Data.SQL.Instance.Variable |\n    where {$_.Name -eq 'SQLAdmin'}\n$node.Value = $SQLAdmin\n\n$node = $xml.Data.VMs.VM |\n    where {$_.Type -eq 'DNSServerVM'}\n$node.VMName = $DNSServerVMName\n\n$xml.Save($path)<\/code><\/pre>\n<p>The final xml will look like below.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated-300x159.png\" alt=\"Image updated\" width=\"300\" height=\"159\" class=\"alignnone size-medium wp-image-907\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated-300x159.png 300w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated-1024x543.png 1024w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated-768x407.png 768w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated-1536x815.png 1536w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2023\/03\/updated.png 2031w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>Till Then, Happy Scripting \ud83d\ude42<\/p>\n<p>Follow more PowerShell posts <a href=\"https:\/\/devblogs.microsoft.com\/powershell-community\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This posts explains how to update XML files using PowerShell<\/p>\n","protected":false},"author":115450,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[53,3,11],"class_list":["post-895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-configuration","tag-powershell","tag-xml"],"acf":[],"blog_post_summary":"<p>This posts explains how to update XML files using PowerShell<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/115450"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=895"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/895\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}