Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script to update XML file content

Please help me create a Powershell script that will go through an XML file and update content. In the example below, I want to use the script to pull out and change the file path in the Config.button.command example. Change C:\Prog\Laun.jar to C:\Prog32\folder\test.jar. Please help. Thanks.

<config>  <button>   <name>Spring</name>   <command>      C:\sy32\java.exe -jar "C:\Prog\Laun.jar" YAHOO.COM --type SPNG --port 80   </command>   <desc>studies</desc>  </button>  <button>   <name>JET</name>     <command>        C:\sy32\java.exe -jar "C:\Prog\Laun.jar" YAHOO.COM --type JET --port 80     </command>   <desc>school</desc>  </button> </config> 
like image 273
user2359932 Avatar asked May 07 '13 21:05

user2359932


People also ask

How do I update XML value in PowerShell?

To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method. We have below the XML file from the link stored in SampleXml. XML on C:\Temp location. The above commands will load the XML file and select node with attribute value 'bk102'.

How do I update an XML file?

To update data in an XML column, use the SQL UPDATE statement. Include a WHERE clause when you want to update specific rows. The entire column value will be replaced. The input to the XML column must be a well-formed XML document.


1 Answers

I know this is an old post but this may help others so...

If you specifically know the elements that you are looking for then you can simply specify the element like so:

# Read the existing file [xml]$xmlDoc = Get-Content $xmlFileName  # If it was one specific element you can just do like so: $xmlDoc.config.button.command = "C:\Prog32\folder\test.jar" # however this wont work since there are multiple elements  # Since there are multiple elements that need to be  # changed use a foreach loop foreach ($element in $xmlDoc.config.button) {     $element.command = "C:\Prog32\folder\test.jar" }      # Then you can save that back to the xml file $xmlDoc.Save("c:\savelocation.xml") 
like image 113
ThisWholeDev Avatar answered Sep 20 '22 22:09

ThisWholeDev