Edit multiple Config Files

I have multiple webconfig files that I need to edit.
Is there a smart way to do this with scripting?

I have a csv file with two columns (User and NewPw) and I need to edit this line which have different password and userName in each WebConfig file
<identity impersonate="true" password="kjsjhdsj-dfsf343-3432efd5-ijt548" userName="DOMAIN\User1" />

Open in new window

Any good ideas on how to do this through Powershell? RegEx maybe?
Kasper KatzmannSeniorkonsulentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dorababu MSenior Software EngineerCommented:
May be you can write a module as follows and call that for each config file. The following I used to update the database instance with the required one. Try changing this as per your need

function ModifyConnectionString
    param (
        [String] $XmlaFile,
        [String] $SqlServerInstance = "(local)"

        ## Preserve caller preferences.  Used to allow -Verbose flag to carry throughout execution.
        Try { Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState } Catch { <# Do Nothing #> }
        Write-Verbose "Begin function:  '$($MyInvocation.MyCommand)' ..."

        $ModifySettings = [xml](Get-Content $XmlaFile)
        $CubeName = $ModifySettings.Batch.Alter.Object.DatabaseID

        Write-Output "Modifying the connectionstring dynamically"

        #looping through all the child nodes that matches the node connectionstring and modifying the connectionstring

        $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ChildNodes | Where-Object Name -Match 'ConnectionString' | ForEach-Object {
            $DataBase = $_.'#text'.Split(";").Split("=")
            $DataBase = $DataBase[$DataBase.Length - 1]

            $ConnectionString = "Provider=SQLNCLI11.1;Data Source=$SqlServerInstance;Integrated Security=SSPI;Initial Catalog =$Database"

            $_.'#text' = "$ConnectionString"


        Write-Output "Changed the connectionstring"

        Write-Output "`n Modifying the ImpersonationMode and Account to defaults"

        $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ImpersonationInfo.ImpersonationMode = "Default"

        if($ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ImpersonationInfo.Account -ne $null)
		    $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ImpersonationInfo.Account = "Administrator"

        Write-Output "`n saving the modifications"
        Write-Output "`n saved the modifications"

        Write-Verbose "End function:  '$($MyInvocation.MyCommand)'"

Open in new window

Dorababu MSenior Software EngineerCommented:
Try this by looping your web.config files, as said write a module and pass the file name and required parameters to update the file

$appConfig = [xml](cat C:\Desktop\web.config)
#$appConfig.configuration | Get-Member -Force

$appConfig.configuration.ChildNodes | Where-Object Name -Match 'system.web' | ForEach-Object {

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dorababu MSenior Software EngineerCommented:
Loop through files and changing

$files = Get-ChildItem -Path "D:\WebConfig" -Filter "*.config" -Recurse
Write-Host $files
foreach($file in $files)
    $configFile = $file.FullName
    $appConfig = [xml](cat $configFile)
    $appConfig.configuration.ChildNodes | Where-Object Name -Match 'system.web' | ForEach-Object {


Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.