Running PowerShell in Task Scheduler

Troubleshooting scheduled tasks is a pain in the rear, because you can’t really see what’s going on. These are some things you may want to check:

  • Check that your commandline works in principle, e.g. by running it from CMD (in your case try running powershell.exe -File "D:\script.ps1"). If that gives you any errors you need to fix those first.

  • If you intend to run the task as a particular user, start CMD as that user and run the same commandline to check if the user has all the permissions required for whatever the script is doing.

  • Check if your task actually terminated or if the process is still running (via Process Explorer, Get-Process, Task Manager, …).

  • Check the Last Run Result for the exit code of the command.

  • Enable the history for your scheduled tasks (Action → Enable All Tasks History). That will give you at least some information about what the task is doing, whether it starts at all, and if/which errors occurred. You need administrative rights to enable the task history.

  • Check the eventlog for errors/warnings correlating with the task run.

  • Add logging statements to the script you’re running to record progress information. Personally I prefer logging to the eventlog, because that avoids filesystem permissions issues.

    Write-EventLog -LogName Application -Source EventSystem -EventID 100 -EntryType Information -Message 'Your log message.'
    

    If you have admin privileges on the system you can register an event source of your own and use that in the above log statement instead of abusing an existing source like EventSystem:

    New-EventLog -Source MyEventSource -LogName Application
    

Further help will depend heavily on the findings you got following these steps as well as your actual script code.

Leave a Comment