Powershell script to update web.config file


Function/script to update web.config file. The script can be downloaded from the Microsoft Script Center Repository. Script can:

  • Replace text within web.config file preserving case
  • Remove sections/nodes from web.config file
  • Comment out sections in web.config file

The script will make a backup copy of the web.config file before updating it.

Example:

Update-Web.Config -Destination E:\WebSites\Mywebsite1.com -CommentSection configSections -Verbose

Update-WebConfig1

The script comments out the ‘configSections’ section

Update-WebConfig2

Example:

Update-Web.Config -Destination E:\WebSites\Mywebsite1.com -OldPath D:\backme.up\websites\www.MyWebsite1.com\www -Verbose

Update-WebConfig3

The script replaces all occurrences of the string ‘D:\backme.up\websites\www.MyWebsite1.com\www’ with ‘E:\WebSites\Mywebsite1.com’

Example:

Update-Web.Config -Destination E:\WebSites\Mywebsite1.com -RemoveSection ‘/configuration/configSections’ -Verbose

Update-WebConfig4

The script removes ‘/configuration/configSections’ section from web.config file

Example:

Update-Web.Config -Destination E:\WebSites\Mywebsite1.com -OldPath D:\backme.up\websites\www.MyWebsite1.com\www -CommentSection configSections -RemoveSection ‘/configuration/runtime’ -Verbose

Update-WebConfig5

The script replaces all occurrences of the string ‘D:\backme.up\websites\www.MyWebsite1.com\www’ with ‘E:\WebSites\Mywebsite1.com’, comments out ‘configSections’ section, and removes ‘/configuration/runtime’ section from web.config file

Advertisements

One response

  1. Ritesh Kumar

    I am getting the error “Exception calling “SelectSingleNode” with “1” argument(s): “Expression must evaluate to a node-set.””

    May 11, 2017 at 6:17 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s