Using PowerShell to Modify AppSettings in Web.config

I’ve been working on automating some of our builds and deployments at work, which means I’ve dipped back into PowerShell land. And though I find it frustrating at times, I like PowerShell in the area of devops. It’s a pretty good tool to have in your toolbelt when needed.

Anyway, in one of our deployment scripts I needed to update an appSettings value for our web.config and app.config files based on other build parameters. Knowing that PowerShell understands native .NET, I came up with this script, aptly named ModifyAppSetting.ps1 (note that this is slightly different than what I wrote about a few years ago):

Which you can then invoke as such:

.\ModifyAppSetting.ps1 ‘path to config file’ ‘app setting key’ ‘app setting value’

For example:

.\ModifyAppSetting.ps1 ‘C:\Projects\MyProject\Web.config’ ‘MyAppSettingKey’ ‘NewAppSettingValue’

The script is fairly straightforward: it loads the config file as an XmlDocument, selects the appSettings node that matches the key, updates it’s value, then saves the file to apply the changes. Done.


One comment

Leave a Reply

Your email address will not be published. Required fields are marked *