powershelltaskscheduler

Unable to Save Log File from Powershell Command in Task Scheduler


I am trying to create a log file from a Powershell task in Task Scheduler but, nothing that I try is working.

Program/script

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments

-ExecutionPolicy Unrestricted -File "C:\Users\Boston\script.ps1" > C:\Users\Boston\script.log

This is not saving the output of the task to a file.


Solution

  • You don't get any output because you are passing the redirection as an argument to powershell.exe because the redirection isn't interpreted by a cmd prompt. e.g.

    Executable:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    

    Argument List:

    -ExecutionPolicy 
    Unrestricted 
    -File 
    "C:\Users\Boston\script.ps1" 
    > 
    C:\Users\Boston\script.log
    

    In order to log output, the best way is to do it within the script. But, you can work around it by running the script as a -Command and then use PowerShell itself to redirect the output for you:

    Arguments:

    -ExecutionPolicy Unrestricted -Command "& C:\Users\Boston\script.ps1 > C:\Users\Boston\script.log"