1

How to replace the values in nested xml using Powershell?

How do i replace the values of "Racers", "Pacers" "Losers" from "false" to "True" using PowerShell ?

<Tester>
<TesterSection>
 <section name="TestingApp" type="Amazon,Google" />
</TesterSection>
<application>
 <add key="Minimum" value="1" />
 <add key="Maximum" value="true" />
 <add key="Runners" value="N/A" />
 <add key="Racers" value="false" />
 <add key="Pacers" value="false" />
 <add key="Losers" value="false" />
</application>  
</Tester> ```
1
  • Your question is a bit unclear: are you changing these three because of the key values or because their value values are all false; that is - what if Runners was also false, would its value have to change, too, or just these three? Commented May 2, 2020 at 10:44

2 Answers 2

4

Another suggestion is to use xpath. Here is a working snipper for your example

[xml]$xml = @"
<Tester>
    <TesterSection>
        <section name="TestingApp" type="Amazon,Google" />
    </TesterSection>
    <application>
        <add key="Minimum" value="1" />
        <add key="Maximum" value="true" />
        <add key="Runners" value="N/A" />
        <add key="Racers" value="false" />
        <add key="Pacers" value="false" />
        <add key="Losers" value="false" />
    </application>  
</Tester>
"@

foreach ($key in $("Racers","Pacers","Losers")) {
    $xml.selectNodes("//application/add[@key='$key']") | %{ $_.value = "true" }
}
$xml.Save("c:\temp\FileAfter.xml")
Sign up to request clarification or add additional context in comments.

1 Comment

Now it works, I replace " by ' and ' by " in the XPath. It's a nice way.
2

You can try this

# Simulate the XML in a string
$text = @"
<Tester>
<TesterSection>
 <section name="TestingApp" type="Amazon,Google" />
</TesterSection>
<application>
 <add key="Minimum" value="1" />
 <add key="Maximum" value="true" />
 <add key="Runners" value="N/A" />
 <add key="Racers" value="false" />
 <add key="Pacers" value="false" />
 <add key="Losers" value="false" />
</application>  
</Tester>
"@

$xml = [xml]$text
# You can load the file with
# $xml = [xml] "c:\temp\thefile.xml"

# List of keys to change
$tochange = "Racers","Pacers","Losers"

foreach ($add in $xml.Tester.application.add)
{
  # value before
  $add.value
  # modification
  if ($tochange -contains $add.key)
  {
    $add.value= "true"
  }
  # name after
  $add.value
}
# Or in a sigle line
#$xml.Tester.application.add | % {if ($_.value -eq "false"){$_.Value ="true"}}
$xml.Save("c:\temp\FileAfter.xml")

4 Comments

Can i not use something like this? $xml = [xml]$text $xml.configuration.appSettings.add | % {if ($_.key -eq "Racers","Pacers","losers"){$_.Value ="true"}}
Not -eq with a list but you can create a list ans use -contains.
-contains is not replacing the values :(
I edit my answer to show you the way I use -contains

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.