title: How to change values of web.config file using powershell
body: <p>Hi I am trying to change the values of a connection string for a web.config file but getting error:</p>
  
  <blockquote>
    <p>The property <code>'connectionString'</code> cannot be found on this object. Verify that the property exists and can be set.</p>
  </blockquote>
  
  <p>Here's the script I'm using:</p>
  
  <pre><code>$webConfig = 'C:\Users\test\Desktop\web\web.config'
  $doc = (Get-Content $webConfig) -as [Xml]
  $obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'CommandTimeOut'}
  $obj.value = '60'
  $config = [xml](gc $webConfig)  
  $con= $config.configuration.connectionStrings.add|where-object{$_.name -eq "password"};
  
  $con.connectionString = $con.connectionString -replace "123456", "admin1234"
  
  $doc.Save($webConfig)
  </code></pre>
  
  <p>I've modified the code as below, but it's still not working and I'm getting the same error. </p>
  
  <pre><code>$cfg = [xml](gc $webConfig) 
  $con= $cfg.configuration.connectionStrings.add|where-object{$_.name -eq "password"};
  $cfg.configuration.connectionStrings.add.connectionString=   
  $cfg.configuration.connectionStrings.add.connectionString -replace "123456","admin123"
  $doc.Save($webConfig)
  </code></pre>