Update XML files using PowerShell


 

There are many blogs on internet already speaking about updating XML files in PowerShell, but I felt need of one consolidated blog where complex XML files can also be updated with long complex hierarchy of XML nodes and attributes.

Below is an XML example which we will try in this blog to update at various level of node hierarchy.

Sample 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= »DomainOldSQlAdmin » /> <Variable Name= »SQLUser » Value= »domainsqluser » /> </Instance> </SQL> <VMs> <VM Type= »ClientVM »> <VMName>ClientVM</VMName> </VM> <VM Type= »DNSServerVM »> <VMName>OldDNSServer</VMName> </VM> </VMs> </Data>