A bit late but it may help others. Redirecting powershell output in task scheduler is a bit tricky.
If you pass the script using the -file
parameter, the redirection is always handled by the task scheduler CLI which has absolutely no idea of what $(Get-Date -format "yyyyMMdd")
means and tries (unsuccessfully) to interpret it as a filename. More, as $(Get-Date -format "yyyyMMdd")
contains space chars, the CLI splits it into multiples parameters adding more mess to the command line. If you do want to use -file
, you have to rebuild the date from %date%
On the other hand, if you replace -file
by -command
and quote >
with ^
to hide it from the task scheduler CLI, the redirection is handled by powershell
which understands $(Get-Date -format "yyyyMMdd")
. Be aware the the task scheduler CLI interprets "
in the same way that CMD
does so it will remove them. In that very case, it's not a problem as the -f
of Get-Date
is waiting for a [String]
argument but if for instance, you use $((get-date).tostring("yyyyMMdd"))
, you will get an error. So just replace "
by '
(eg. $((get-date).tostring('yyyyMMdd'))
)
To summarize, your parameters should read
-command C:\Users\Admin\Desktop\scripts\automatexml_weu.ps1 -Verbose ^> C:\Users\Admin\Desktop\scripts\xml_script_logs\xml_script_output_$(Get-Date -format 'yyyyMMdd').log
One last point, if you only redirect stream 1, you'll only get what your script sent to the output stream and will never get the content of the error/ warning/ verbose/ streams. If you are interested in them (I suppose you are as you have -verbose
in you command), juste replace ^>
by ^*^>
in the command parameters.
-command C:\Users\Admin\Desktop\scripts\automatexml_weu.ps1 -Verbose ^*^> C:\Users\Admin\Desktop\scripts\xml_script_logs\xml_script_output_$(Get-Date -format 'yyyyMMdd').log