title: Using powershell to batch convert docx to pdf
body: <p>I'm attempting to use powershell to batch convert a lot of docx into pdf, into a different directory while maintaining the folder structure of the root. </p>
  
  <p>I have the script working, however around 1 out of every 10 documents word pops up a "SaveAs" dialog, which i do not understand prompting me to save the docx file, although i have visible set to false.</p>
  
  <pre><code>#Stage the files
  $sourceDir = "C:\Documents\"
  $targetDir = "C:\Temp_Stage\"
  
  Get-ChildItem $sourceDir -filter "*.doc?" -recurse |  foreach{ 
      $targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length); 
      New-Item -ItemType File -Path $targetFile -Force;  
      Copy-Item $_.FullName -destination $targetFile 
  } 
  
  #Convert the files
  $wdFormatPDF = 17
  $word = New-Object -ComObject word.application
  $word.visible = $false
  $folderpath = "c:\Temp_Stage\*"
  $fileTypes = "*.docx","*doc"
  
  Get-ChildItem -path $folderpath -include $fileTypes -Recurse |
  foreach-object {
   $path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
   $doc = $word.documents.open($_.fullname)
   $doc.saveas([ref] $path, [ref]$wdFormatPDF)
   $doc.close()
   }
   $word.Quit()
  </code></pre>
  
  <p>Is there a way to suppress all word dialogs / warning / errors it should be a fairly automatic process that has ended up being pretty manual process. </p>