Output file is empty when running powershell script from Task Scheduler

ba flag

I have a powershell script, that write http response body to output.

When I run it from command line like this, it writes the response to Emails.log file

powershell "./InvokeMyApi.ps1 /api/emails/SendEmails" > Emails.log

However, when I invoke it from Task Scheduler, it only writes empty file.

To rule out missing permission, I granted write access to the folder to everyone

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="">
    <Principal id="Author">
  <Actions Context="Author">
      <Arguments>"./InvokeIsysApi.ps1 /api/emails/SendEmails" &gt; Emails.log</Arguments>

How to get output from my powershell script to a file?

us flag

Are you sure the script itself is definitely running successfully via Task Scheduler, so there's output actually being generated to be piped to the text file?

Could be the old issue of the context that it's being run in via task schedule not having execution policy set to allow it to run (which I don't think shows in Task Scheduler as a fail, as Powershell was successfully called, but the failure happens afterwards). Try setting the task to run it like this instead

powershell -executionpolicy bypass -command "./InvokeMyApi.ps1 /api/emails/SendEmails" > Emails.log

which will then ensure the execution policy has been set regardless of the profile it's running under.

Personally I'd also specify the full folder paths to the files as well, rather than just relying on Task Scheduler running in the correct folder at the time, but since it is at least generating an empty file I'd guess that's not your issue.

Liero avatar
ba flag
It does run, I see it in task's history tab: `Task Scheduler successfully completed task "blabla" , instance "nlabla" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE" with return code 0.`
Keith Langmead avatar
us flag
Another thing you could check is within the scheduled task, in the General tab, what is "Configure for:" at the bottom set to. I've been caught out in the past where the server is newer, but for whatever reason Task Scheduler is defaulting to a much older "Configure for" version (like 2008!), which then doesn't allow some of your functionality. In that case, updating it to use the current version may resolve it.
I sit in a Tesla and translated this thread with Ai:


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.