Redirect script output to file in task scheduler RRS feed

  • Question

  • Hi, I have a script running in a scheduled task and I was using this "trick" to redirect the script's output to a file:

    powershell.exe -c "powershell.exe -c .\test.ps1 -verbose >> .\test.log 2>&1"

    And it was working on a windows 7 with PS v.2, but on a windows 10 with PS 5, it doesn't work anymore.

    I googled around a lot but I couldn't find any solution, I have found also in this forum some posts about it:

    But they are quite old (2013) and the suggested solutions don't work with windows 10 and PS 5.

    Just to avoid some misunderstanding, the scripts can be executed also manually and in this case the output should be in the console, so that the users can see immediately what is the output and if there is an error. But when they are executed as scheduler task we want to redirect the output to a log file, so that we can check the log file for errors and problems. Therefore we don't want to write always the output into a log file, but just redirect the output to a log file when the scripts are executed automatically by the task scheduler. Moreover we don't want to hardcode any log file name in the script, because the same script can be executed with different parameters and we want to log the output in different files.

    • Edited by spamme1 Monday, March 20, 2017 12:19 PM
    Monday, March 20, 2017 12:15 PM

All replies

  • The scheduled task cannot redirect that way.  You will have to redirect to a file inside of the PowerShell script.  It is PowerShell that supports that redirection.


    Monday, March 20, 2017 7:29 PM
  • Thanks for the reply, but isn't what I'm doing by just passing a string command to the scheduled task?

    powershell.exe -c .\test.ps1 -verbose >> .\test.log 2>&1
    Moreover, why it is working in a console but not in a scheduled task?

    • Edited by Aquilax Tuesday, March 21, 2017 12:57 PM
    Tuesday, March 21, 2017 12:56 PM
  • Because of the console.  Task Sched is not a console.


    Tuesday, March 21, 2017 4:58 PM
  • Yes, but it was working in Windows 7 and PS v.2, now it creates the log file (test.log) but it is always empty, which means something changed in the output redirection.
    Wednesday, March 22, 2017 8:36 AM
  • What I had to do to solve this was create a .cmd file that is the "action" and that in turn invokes powershell... and >> redirection on the command line works there. So the scheduled task just says to run the .cmd, and the .cmd contains one line that looks something like this (all on one line): 

    PowerShell -Version 2.0 -noninteractive -NoLogo -ExecutionPolicy RemoteSigned %userprofile%\savemanydumps.ps1  2>&1 >> %userprofile%\savemanydumps.log 

    (I find it absurd that there isn't a built-in logging facility in Scheduled Tasks, and that we have to jump through these hoops to get it. "Settings" in Scheduled Tasks should have an entry for a log file and if specified, any process started under Scheduled Tasks should have its Standard Output and Standard Error streams sent there.) 

    Monday, February 19, 2018 3:11 AM