title: Powershell - Read, Alter Line, Write new line over old in Text Document
body: <p>I am reading in line by line of a text file.  If I see a specific string, I locate the first and last of a specific character, use two substrings to create a smaller string, then replace the line in the text file.</p>
  <p>The difficult part: I have the line of text stored in a variable, but cannot figure out how to write this new line over the old line in the text document.</p>
  <p>Excuse the crude code - I have been testing things and only started playing with PowerShell a few hours ago.</p>
  <pre><code>foreach($line in [System.IO.File]::ReadLines(&quot;C:\BatchPractice\test.txt&quot;))
  {
         Write-Output $line
         if ($line.Contains(&quot;dsa&quot;)) {
             Write-Output &quot;TRUEEEEE&quot;
             
         }
         $positionF = $line.IndexOf(&quot;\&quot;)+1
         $positionL = $line.LastIndexOf(&quot;\&quot;)+1
         $lengthT = $line.Length
         Write-Output ($positionF)
         Write-Output $positionL
         Write-Output $lengthT
         if($line.Contains(&quot;\&quot;)){
             Write-Output &quot;Start&quot;
              $combine = $line.Substring(0,$positionF-1) + $line.Substring($postionL,($lengthT-$positionL))
              Write-Output $combine
              $line1 = $line.Substring(0,$positionF-1)
              $line2 = $line.Substring($positionL,($lengthT-$positionL))
              $combined = $line1 + $line2
              Write-Output $combined
              Write-Output &quot;Close&quot;
         }
  }```
  </code></pre>