In many cases, we found ourselves in a situation where we need to Read and update XML files.
It’s quite straight forward in Power Shell.
Sample XML File:
Powershell script to read and update XML file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <App> | |
| <Secret></Secret> | |
| </App> |
Powershell script to read and write XML file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $xmlFileName "Path to XML File"; | |
| [xml]$xmlDoc = Get-Content $xmlFileName | |
| $xmlDoc.APP.Secret = "Some Value" | |
| $xmlDoc.Save($xmlFileName) |