title: Powershell script run from task scheduler unable to open word document
body: <p>I am trying to use a powershell script to get the wordcount from a number of word files and then output that to a csv file. This works as expected when run from the powershell prompt, and works when called from the cmd prompt directly or from inside a perl script, however the script fails to work when called as a scheduled task.</p>
  
  <p>This is <strong>not</strong> an ExecutionPolicy issue causing the script to not run at all. The script is running and produces some output, but when launched from task scheduler it is unable to open any word documents.</p>
  
  <p>The relevant powershell code is below:</p>
  
  <pre><code>$folderpath = "C:\some\where\*"
  $fileTypes = "*.docx"
  
  $word = New-Object -ComObject word.application
  $word.visible = $false
  Get-ChildItem -path $folderpath -recurse -include $fileTypes |
  foreach-object `
  {
    $path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
  
    try {
      $doc = $word.documents.open($_.fullname, $confirmConversion, $readOnly, $addToRecent, $passwordDocument)
    } catch {
      "FROM CATCH UNABLE TO OPEN $($_.fullname)" &gt;&gt; $wordCountFile
    }
    if($doc) {
      $wordCount = $doc.ComputeStatistics("wdStatisticWords")
      "$($_.name), $wordCount"  &gt;&gt; $wordCountFile
      $doc.close([ref]$false)
    } else {
      "UNABLE TO OPEN $($_.fullname)" &gt;&gt; $wordCountFile
    }
  
  } #end Foreach-Object
  $word.Quit()
  </code></pre>
  
  <p>(Note that alternative methods people cite for getting the word count from word documents do not actually return the correct count as this method does <em>when it works</em>.)</p>
  
  <p>When run as a scheduled task (set to run with highest privileges and as an administrator user) using:</p>
  
  <pre><code>cmd /c start PowerShell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\path\to\script\CountsInWords.ps1"
  </code></pre>
  
  <p>or when run with a similar command from a perl script launched by a scheduled task (my normal use case) and in both cases the task is set to run with highest privileges the powershell script does not work correctly.</p>
  
  <p>The catch block is apparently never reached, as the print statement never makes it to the file, however the $doc is always null.</p>
  
  <p>Additionally, when started as a task the script leaves a word process open and using a 100% cpu for 1 thread which will eventually cripple my machine.</p>
  
  <p>To summarise:</p>
  
  <p>run script as human (no matter how many levels of indirection) -> works perfectly</p>
  
  <p>run script from task (as administrator user) -> script runs but cannot access word documents, also unable to stop word process despite always hitting the $word.Quit line.</p>
  
  <p>EDIT: With @TheMadTechnician's advice about office first time startup for a new user requiring some details: On further inspection, looking at the processes in task manager, I don't see the word processes unless I click on "show processes from all users", but then they show up, but have the user listed as me. How can a process be both explicitly listed as me, but count as another user?</p>
  
  <p>Attempting to set <code>$word.visible = $true</code> in the script didn't actually make anything appear when launched from task scheduler, so I don't know either how to verify that it is waiting for input, or how to give it that input as the correct user to make it go away...</p>