title: Using PowerShell in a .bat file, replace a string with multiple strings
body: <p>I'm using a .baat to move several files into another folder, but before the actual move part, I want to replace the LAST line (it is a known line), for example I have a file output.txt like this:</p>
  
  <pre>HEADER
      BODY
   FOOTER</pre>
  
  <p>Using this snippet of code:</p>
  
  <pre><code>powershell -Command "(gc output.txt) -replace 'FOOTER', 'ONE_MORE_LINE `r`n FOOTER' | Out-File output.txt"
  </code></pre>
  
  <p>The return that I expected was </p>
  
  <pre>HEADER
      BODY
   ONE_MORE_LINE
   FOOTER</pre>
  
  <p>But what I got was:</p>
  
  <pre>HEADER
      BODY
   ONE_MORE_LINE `r`n FOOTER</pre>
  
  <p>I've tried:</p>
  
  <ul>
  <li><code>\n</code></li>
  <li><code>&lt;br&gt;</code></li>
  <li><code>"`r`n"</code></li>
  <li><code>"`n"</code></li>
  <li><code>echo ONE_MORE_LINE &gt;&gt; output.txt; echo. &gt;&gt; output.txt; echo  FOOTER &gt;&gt; output.txt"</code></li>
  </ul>
  
  <p>This last one got close, but the result was some broken characters.</p>
  
  <p>Other suggestions besides the PowerShell are welcome. I'm only using it because it was an easy get way to do the adding lines and replace it.</p>
  
  <p>EDIT :
  Tried this command</p>
  
  <pre><code>powershell -Command "(gc output.txt) -replace 'FOOTER;', ""ONE_MORE_LINE `r`n FOOTER"" | Out-File output.txt "
  </code></pre>
  
  <p>And returned this error:</p>
  
  <pre>A cadeia de caracteres não tem o terminador: ".
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString </pre>
  
  <p>EDIT2 - Possible Solution:</p>
  
  <p>I realized that using the PowerShell command altered the encoding of the file, breaking the <code>echo ONE_MORE_LINE</code>, and using the suggestion from @AnsgarWiechers, I made this code</p>
  
  <pre><code>findstr /v "FOOTER" output.sql &gt; new_output.sql
  TYPE new_output.sql &gt; output.sql
  del new_output.sql
  ECHO. &gt;&gt; %%f
  ECHO ONE_MORE_LINE &gt;&gt; %%f
  ECHO FOOTER &gt;&gt; %%f
  ECHO. &gt;&gt; %%f
  </code></pre>
  
  <p>What it does is using the commant <code>findstr /v</code> "FOOTER" I look for all lines that are not FOOTER in the file output.sql and Write it on new_output.sql</p>
  
  <p>Then I <code>TYPE</code> it back to the original file, and <code>DEL</code> the new_output.sql</p>
  
  <p>Then I <code>Echo</code> all the lines I need right under it.</p>
  
  <p>It works BUT, for big files I think that re-writing it twice will take a lot of time, but I can't figure an other solution.</p>