Specifying log file for a scheduled powershell script
Sebastian Wright
I have created a scheduled task in windows to run a powershell script at specific time. As you can see, in the action page, I have specified powershell path as the program and my script as the argument. The scheduled task works fine.
I would like to include my own log file that prints the output of my script there. How can I do that?
2 Answers
You can use a redirection to redirect the output of your powershell script in a file.
However, redirection not always work perfectly when using directly powershell.
So you can use cmd.exe to call powershell and redirect the global outpout to a log file.
I propose you the following parameters for your scheduled task :
Program/script : C:\Windows\System32\cmd.exe
Add arguments : /c "powershell.exe PATH_OF_YOUR_PS_SCRIPT.ps1" > PATH_OF_LOG_FILE.log
The symbol > is used to redirect the output of the command to the file next to it.
TryStart-TranscriptorStart-Transcript -Path c:\temp\log.txt -Appendon the beginning of your .ps1 file andStop-Transcriptin the end
It will start logging all output + some additional information.