0

I have the following xml file.

<Objects>
  <Object>
    <Property Name="Browser">Firefox</Property>
    <Property Name="PDF">Adobe Reader</Property>
</Object>

I want to be able to update the word firefox using powershell script.

This is the powershell script that am working and is not working.

$xmlDoc = [XML](Get-Content "c:\Windows\personalsettings\PersonalSettings.xml")
 foreach ($item in  $xmlDoc.Objects.Object.Property)
 {
    $item.Name = 'Chrome'
 }
 $xmlDoc.Save("c:\Windows\personalsettings\PersonalSettings.xml")
PRobles
  • 37
  • 4

1 Answers1

0
[xml]$XML = @"
<Objects>
  <Object>
    <Property Name="Browser">Firefox</Property>
    <Property Name="PDF">Adobe Reader</Property>
  </Object>
</Objects>
"@

$XML.SelectSingleNode('//Property[@Name="Browser"]')
$XML.SelectSingleNode('//Property[@Name="Browser"]').InnerText = "TEST"
$XML.SelectSingleNode('//Property[@Name="Browser"]')

Results will be

> Name    #text  
> ----    ----- 
> Browser Firefox
> Browser TEST

What you are looking for is XPATH

Works like this

//Path Or NodeName[@AttributeName="Value Of Attribute"]

Once you get the Node you can then edit that node how you see fit.

ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • Thanks for the example. However, when running your code is replace the word browser with the new text instead of replacing the value after the word browser. – PRobles Feb 28 '20 at 18:51
  • No its not. In my example it would be impossible for it to change the word browser. Make sure you are changing the `InnerText` – ArcSet Feb 28 '20 at 18:54
  • $xmlDoc = [XML](Get-Content "c:\Windows\personalsettings\PersonalSettings.xml") $XML.SelectSingleNode('//Property[@Name="Browser"]').InnerText = "TEST" $xmlDoc.Save("c:\Windows\personalsettings\PersonalSettings.xml") You cannot call a method on a null-valued expression. At line:3 char:1 + $XML.SelectSingleNode('//Property[@Name="Browser"]').InnerText = "TES ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull – PRobles Feb 28 '20 at 19:05
  • If you look at the example you gave... you didnt really have real XML. you forgot to end the example with a `` Meaning that there is no `//Property[@Name="Browser"]` because you did not have finished XML. – ArcSet Feb 28 '20 at 19:09
  • Again, here is the xml file: Firefox Adobe Reader – PRobles Feb 28 '20 at 19:14
  • Your calling your xml `$xmlDoc` but when you do the XPATH you are using `$XML`. `$XML` is null you need to call the xpath on `$XMLDOC`. `$XMLDOC.SelectSingleNode('//Property[@Name="Browser"]').InnerText = "TEST"` – ArcSet Feb 28 '20 at 19:17
  • That was it. Thanks Thanks so much! On a side note, do you know why every time the code runs, there is a strange message poping out saying "object not set to an instance of an object" – PRobles Feb 28 '20 at 19:29
  • Thats not enough information to help you. I mean I would need to see code and stuff. Did you make a post? if so whats the Post. If not make one so i can look. – ArcSet Feb 28 '20 at 19:30
  • it is working now. Thanks – PRobles Feb 28 '20 at 19:47