title: Why do PS1 calls inside a PS1 Script run when executed in Powershell, but are not executed when run as a Windows Scheduled Task?
body: <p>I am running a Powershell script (TaskScript.ps1) using Powershell v4.0 (from Get-Host cmdlet) w/ File Version: 6.3.9600.17415 (Windows Explorer File Properties.) I am trying to run this script from a regularly scheduled task on Windows Server 2012 R2; the TaskScript.ps1 does this:</p>
  <pre><code>&amp; .\OtherScript1.ps1
  #... log messages to LogFile ...
  &amp; .\OtherScript2.ps1
  #... log more messages to LogFile ..
  </code></pre>
  <p>When invoked manually in Powershell, the entire TaskScript.ps1 -- including calls to OtherScript1.ps1 and OtherScript2.ps1 -- execute as expected and logging to LogFile is executed as desired; that is, <strong>after</strong> OtherScript.ps1 and OtherScript2.ps1 complete.</p>
  <p>However when TaskScript.ps1 is run from a Windows Scheduled Task, the script is run and LogFile is updated immediately without invoking OtherScript1 or OtherScript2 executing.</p>
  <p>I've checked that:</p>
  <ul>
  <li>LogFile is clear before manually running the script and manually executing the Task</li>
  <li>running the script manually executes all of the included scripts as
  expected (each subscript has its own logging which has been checked)</li>
  <li>LogFile is written to (at the appropriate times) when executed manually (I've watched the output as it happens)</li>
  <li>The Scheduled Task is running and invoking TaskScript.ps1 (LogFile is updated whenever the task is run)</li>
  <li>TaskScript.ps1 is not executing other scripts when run through Task (regardless of if Task is run manually or is scheduled) -- OtherScript1.ps1 and OtherScript2.ps1 have logging that is not triggered when the Task is run, but is triggered when TaskScript.ps1 is manually run from directly within Powershell. <code>ie. powershell.exe .\TaskScript.ps1</code></li>
  <li>LogFile is written to immediately when executed through the Task. No delay for the other two scripts; as happens when executing within Powershell.</li>
  </ul>
  <p>I've tried a few methods for invoking the scripts within TaskCript.ps1 including the call operator syntax above, the Start-Process cmdlet and dot sourcing (which could work in this case, but is not preferred).</p>
  <p>I've also tried temporarily adjusting the Execution Policy on the Scheduled Task using <code>-ExecutionPolicy Bypass</code> argument in my Task, thinking maybe the calls couldn't be invoked from within a script because of execution policy.</p>
  <p>Some user <a href="https://studiooflegend.atlassian.net/wiki/spaces/SCK/pages/584286209/Design+Specifications?streamsSourceProduct=jira" rel="nofollow noreferrer">here</a> had an issue with Windows Task Scheduler not liking script file off of local drive. In case it's relevant, my TaskScript.ps1, OtherScript1.ps1 and OtherScript.ps2 scripts (and others) are located on a local VHD (virtual disk) at P:\PathTo\Scripts\TaskScript.ps1.</p>
  <p>This is what my task looks like:</p>
  <pre><code>Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  Add arguments (optional): -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command &quot;&amp; Path\To\TaskScript.ps1&quot;
  </code></pre>
  <p>Why won't OtherScript1.ps1 and Otherscript2.ps1 execute if TaskScript.ps1 is run from a Windows Scheduled Task but will run just fine if invoked manually in Powershell?</p>