title: Editing a file in a windows batch file using powershell
body: <p>I am attempting to edit a configuration file using a batch script. I looked around and I believe powershell is the way to go here. I have zero experience with powershell so I'm guessing that the syntax is what is causing me problems.</p>
  
  <p>Here is what the file looks like now (This section is located in the middle of the file)</p>
  
  <pre><code>    &lt;!--add key="MinNumCycles" value="25"/--&gt;
      &lt;!--add key="MaxNumCycles" value="40"/--&gt;
  </code></pre>
  
  <p>Here's what I want it to look like</p>
  
  <pre><code>    &lt;!--add key="MinNumCycles" value="25"/--&gt;
      &lt;!--add key="MaxNumCycles" value="40"/--&gt;
  
      &lt;!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/--&gt;
      &lt;add key="RerunMode" value="0"/&gt;
  </code></pre>
  
  <p>Here's what I'm trying to do in my batch file that I need help with</p>
  
  <pre><code>SET pattern=&lt;!--add key="MaxNumCycles" value="40"/--&gt;
  SET textToAdd1=&lt;!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/--&gt;
  SET textToAdd2=&lt;add key="RerunMode" value="0"/&gt;
  SET filename=Software.exe.config
  
  powershell -Command "(gc %filename%) -replace "%pattern%", "$&amp;`n`n%textToAdd1%"'n"%textToAdd2%" | sc %filename%"
  </code></pre>