Score:0

update XML duplicate named section with Powershell

in flag

Using Powershell, I would like to update Outlook's compatible status to True

<default>
  <application>Excel.exe</application>
  <compatible>True</compatible>

  <application>Outlook.exe</application>
  <compatible>False</compatible>

  <application>Word.exe</application>
  <compatible>False</compatible>
</default>

This post shows how to edit the document if only one 'default.application' instance exists, but the file I am working with has multiple sections named application at that level.

$doc = [xml](Get-Content ./test.xml)

The output of $doc.default.compatible is
True
False
False

How do I modify only the middle value (outlook.exe) to True using Powershell?

Davidw avatar
in flag
When I run `$doc.default.application`, I get the name of the application. `$doc.default.compatible` gives me `True False False`.
in flag
Sorry, my post is wrong. $doc.default.compatible I've updated the post.
Score:0
cn flag

Since you want to change the Compatible Node after the Outlook Node, I implemented to check for it to be the previous value using the PreviousSibling Method before changing value to True.

Updated code:

$docs = [xml](Get-Content ./test.xml)

$doc = $docs.SelectNodes('/default/compatible') 

foreach ($d in $doc){
    if($d.PreviousSibling.OuterXml -eq "<application>Outlook.exe</application>"){
        $d.'#text' = "True"
    }
}

$docs.Save($PSScriptRoot + "\test.xml")
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.