Creating a Powershell script to add a key to multiple web.config files.

by May 22, 2013

I am attempting to create a script that will update multiple web config files (120) with a new add key value. So far the script is what I have so far, however when I run it. It adds it as a hole new node instead of just an add key value. Please help! 





    <add key="Database" value="something" />

    <add key="UUDecodePassword" value="true" />

    <!–<add key="ClientName" value="DEVELOPMENT" />–>

    <add key="ClientFolder" value="Test" />

    <add key="OfflineApplicationHref" value="" />

    <!–<add key="ReportingServiceHref" value="" />–>

    <!–<add key="ReportingServiceUserName" value="" />–>

    <!–<add key="ReportingServicePassword" value="" />–>

    <!–<add key="ReportingServiceDomain" value="" />–>

    <add key="ReportViewerServerConnection" value="ReportViewerServerConnection, APP_CODE" />

    <!–<add key="WebDialerURL" value=""/>–>

    <add key="CacheHost" value="testdb" />

    <add key="aspnet:MaxHttpCollectionKeys" value="5000" />

      <add key="" />





*********************Begin Script ******************************


# Array of files to make changes to, add as many as you like

$filesarray = @("C:UsersDocumentspowershellscriptwebconfigwebsn.config","C:UsersDocumentspowershellscriptwebconfigwebsb.config") 

# Go thru all files

foreach ($filename in $filesarray)


    # Get file + cast as xml

    $xml = [xml](get-content $filename)


    # Backup file before making changes

    $backup = $filename + "-" + (get-date).tostring("yyyy-MM-dd-hh_mm_s")



    $root = $xml.get_DocumentElement();


 # Add a new node

    $node = $xml.createElement("FeedbackFormURL")


    $subnode = $xml.createElement("add")


    $attribute = $xml.CreateAttribute("key")


    $subnode.SetAttributeNode($attribute )


    # Save