title: Powershell and PDFTK merge
body: <p>I have a requirement to merge several thousand pdf's each week which have consistent names eg:</p>
  
  <ul>
  <li>Customer_Invoice_940590.pdf</li>
  <li>Customer_Invoice_940590-details.pdf</li>
  </ul>
  
  <p>In the past I wrote a script that performed this perfectly but alas the script file didn't get backed after a recent OS upgrade. So after rewriting the script (it's been over 2 years) I have it working apart from the merging of the PDFs which Powershell keeps throwing an error unable to find file : <em>pdftk</em></p>
  
  <p>Essentially what I have written pulls the first two PDFs from a folder and moves them to another folder, declare each file as variables, merge the PDFs into a separate output folder, delete variable files. Rinse repeat until all files are merged.</p>
  
  <p>Any thoughts?</p>
  
  <p>Appreciate any help with this and also if there would be a cleaner way to perform this task (I'm sure there is :p)</p>
  
  <pre><code>$pdftk = "C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"
  $inputFolder = "C:\Dump Here"
  $outputFolder = "C:\Combines Files"
  $MiddleOutput = "C:\Middle"
  
  $maxItems = 2
  $CountyCount = Get-ChildItem $inputFolder -File | Measure-Object | % {$_.Count}
  
  If ($CountyCount -gt $maxItems) {
  
      Get-Childitem $inputFolder -filter *.pdf -Recurse |Select-Object -First 2 | Move-Item -Destination $MiddleOutput
  
      $firstPDF = Get-ChildItem $MiddleOutput -Filter *.pdf | Select-Object -first 1
      $lastPDF = Get-ChildItem $MiddleOutput -Filter *.pdf | Select-Object -last 1
  
      pdftk $lastPDF $firstPDF cat Output "$outputFolder\November_$lastPDF"
  
      Remove-Item $firstPDF
      Remove-Item $lastPDF
  }
  else {
      exit
  }
  </code></pre>