Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect PowerShell output when run from Task Scheduler?

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?

like image 663
cmcginty Avatar asked Dec 09 '12 03:12

cmcginty


People also ask

How do I redirect the output of a PowerShell file?

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.

How do I run a PowerShell script in scheduled task with Run as administrator?

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.


1 Answers

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" 
like image 60
cmcginty Avatar answered Sep 26 '22 17:09

cmcginty