title: Search and replace a string in PowerShell
body: <p>I need to search and replace values in a file using the values from another file. For example, <code>A.txt</code> has a string with a value <code>LICENSE_KEY_LOC=test_lic</code> and <code>B.txt</code> contains the string <code>LICENSE_KEY_LOC=</code> or some value in it. Now I need to replace the complete string in <code>B.txt</code> with the value from <code>A.txt</code>. I tried the following but for some reason it does not work. </p>
  
  <pre><code>$filename = "C:\temp\A.txt"
  
  Get-Content $filename | ForEach-Object {
      $val  = $_
      $var  = $_.Split("=")[0]
      $var1 = Write-Host $var'='
      $_ -replace "$var1", "$val"
  } | Set-Content C:\temp\B.txt
  </code></pre>