How to update XML node value using PowerShell?

How can I change the value of node <Test> Test </Test> to <Test> Power </Test> ?

An example :

 <?xml version="1.0"?> <configuration> <appSettings> <add key="DeploymentDate" value="test" /> <add key="DateOfInitialization" value="Vinoj" /> </appSettings> <Test>Test</Test> </configuration> 

This uses the PowerShell script:

 $configuration = "app.config" [xml]$xml = New-Object XML $xml.Load($configuration) $xml.selectnodes("/configuration/Test") = {"UST"} $xml.Save($configuration) 
+9
powershell
Jul 08 '10 at 10:13
source share
1 answer

I don’t know what exactly you want to achieve, but the example should give you an idea:

 $file = 'c:\temp\aa\ServerService.exe.config' $x = [xml] (Get-Content $file) Select-Xml -xml $x -XPath //root/level | % { $_.Node.'#text' = 'test' $_.Node.SomeAttribute = 'value' } $x.Save($file) 

You do not need to use .NET for xpath requests. Just stay with PowerShell (with Select-Xml ).
Also, the xml file is usually downloaded via Get-Content and discarded by [xml] , which creates an XmlDocument and loads the contents of the file.

+21
Jul 08 '10 at 14:17
source share



All Articles