How can I redirect PowerShell output when run from Task Scheduler?
Here is the command that worked for me. I didn't like the idea of redirecting the output in the script, since it would make it difficult to run manually.
powershell -windowstyle minimized -c "powershell -c .\myscript.ps1 -verbose >> \\server\myscript.log 2>&1"
I use the transcript feature to help with this. Just include it in your code and it outputs all (would be) screen content to a log file.
Start-Transcript -path $LogFile -append
<Body of the script>
Stop-Transcript
The following works for me on Windows 7:
powershell -command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log
In the Windows 7 Task Scheduler GUI:
program = "Powershell"
arguments = "-command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log"
Note that "-file" does not work because all parameters after the file name are interpreted as parameters to the file. Note that "2>&1" redirects the error output to the log file as well. Later versions of PowerShell do this in a slightly different way.