Edit config file with PowerShell

Recently I needed to edit machine.config with PowerShell. I thought at first to use config transformations, like I do for application configs (app.config or web.config). But because machine config can have different content (have or don’t have some sections configured) transforming such file becomes complex. I quickly found another solutions for this: xml file editing. PowerShell is very good at it.

Let’s say I have long running transactions on machine. To increase transactions timeout to 1 hour, needs to add or update following configuration in machine.config:

Depending on application platform (32 or 64 bits) and which .NET framework version is used needs to determine which machine config needs to be modified. You also can use .NET RuntimeEnvironment.SystemConfigurationFile property to get machine config location.

Now lets write a function which updates the config file

So that’s all. Here is the working script file. Machine config transformation

3 thoughts on “Edit config file with PowerShell

  1. Donald

    Hello, I need to change the owner of the machine.config file. I am clueless about scripting, is there a way I can modify your powershell file to do this?

    Thank you in advance
    Don

    Reply
  2. Pingback: Modify machine.config (config files) using Powershell « El TecnoBaúl de Kiquenet

Leave a Reply

Your email address will not be published.