When running a simple PowerShell script from Task Scheduler, I would like to redirect the output to a file.
There is a long thread about this very topic here, yet it's not clear if they reached the most appropriate solution in the end. I'm interested if anyone on Stack Overflow has also solved this problem, and how they did it?
There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.
In the Task Scheduler, define the task to run as an account that is a member of the administrators group. To prevent UAC issues, select "run with highest privileges". You don't need to do that. Just have the 1st PS run the script.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With