title: Powershell add header record
body: <p>I have a process in SSIS where I create three files. </p>
  
  <p>Header.txt
  work.txt
  Trailer.txt</p>
  
  <p>Then I use an Execute Process Task to call my Powershell script. I basically need to take the work.txt file and prepend the header record to it (while maintaining integrity of original values in work.txt) and then append the trailer record (which is generated with total row counts, etc.).</p>
  
  <p>Currently I have:</p>
  
  <pre><code>Set-Location "H:\Documentation\Projects\CVS\StageCVS"
  Clear-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
  Get-Content Header.txt, work.txt, Trailer.txt|out-file "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility" -Confirm
  </code></pre>
  
  <p>This is fine in testing where I only had 1000 rows, but now that I have 67,000 rows the process takes forever. </p>
  
  <p>I was looking at the Add-Content cmdlet but I can't find an example where it adds the header. Can someone assist with the syntax on going to the first line in the file and then adding the content before that first line?</p>
  
  <p>many thanks in advance!</p>
  
  <p>Just to clarify: I would like to build off the work.txt file. This si where the majority of the data is already, so instead of rewriting it all to a new file, I think a copy would make more sense. So in theory I would create all three files. copy the work file to say workfile.txt . Prepend header to workfile, append trailer to workfile, rename workfile.</p>
  
  <p><strong>UPDATE</strong>
  This seems to work for the trailer.</p>
  
  <pre><code>Set-Location "H:\Documentation\Projects\CVS\StageCVS"
  #Clear-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
  Copy-Item work.txt workfile.txt
  #Get-Content Header.txt, work.txt, Trailer.txt|out-file "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
  Add-Content workfile.txt -value (get-content Trailer.txt)
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Also tried:</p>
  
  <pre><code>Set-Location "H:\Documentation\Projects\CVS\StageCVS"
  $header = "H:\Documentation\Projects\CVS\StageCVS\Header.txt"
  #Clear-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility.txt"
  Copy-Item work.txt workfile.txt
  #(Get-Content Header.txt, work.txt, Trailer.txt -readcount 1000)|Set-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
  Add-Content workfile.txt -value (get-content Trailer.txt)
  .\workfile.txt = $header + (gc workfile.txt) 
  </code></pre>