0

I am trying to create a log file from a PowerShell task in Task Scheduler, but nothing that I try is working.

Program/script

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments

-ExecutionPolicy Unrestricted -File "C:\Users\Boston\script.ps1" > C:\Users\Boston\script.log

This is not saving the output of the task to a file.

1
  • 1
    How are you running the task and with what privileges? Check the permissions on the file are available to the task. Might be better for you to use a Start-Transcript in your actual script :) Commented Jun 18, 2019 at 15:42

2 Answers 2

2

You don't get any output because you are passing the redirection as an argument to powershell.exe because the redirection isn't interpreted by a cmd prompt. e.g.

Executable:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Argument List:

-ExecutionPolicy 
Unrestricted 
-File 
"C:\Users\Boston\script.ps1" 
> 
C:\Users\Boston\script.log

In order to log output, the best way is to do it within the script. But, you can work around it by running the script as a -Command and then use PowerShell itself to redirect the output for you:

Arguments:

-ExecutionPolicy Unrestricted -Command "& C:\Users\Boston\script.ps1 > C:\Users\Boston\script.log"
Sign up to request clarification or add additional context in comments.

Comments

0

I would do it this way, implied -commmand to interpret the >, and 2>&1 to catch the errors. Otherwise the > doesn't get interpreted unless you're launching it from cmd or a .bat script.

powershell C:\Users\Boston\script.ps1 > C:\Users\Boston\script.log 2>&1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.