Running a Powershell script as a scheduled task

It should have been simple. I have a Powershell script that works as it should, I just need it to run on the hour, every hour. So create a scheduled task, and attach the script. Do a run now, just to make sure it works.

And technically, it works. That’s what the result of the scheduled task says: Task Completed 0x0. But the actual Powershell script hasn’t been actioned.

Once again I confirmed that running the script by itself works. I wrap the code in the script in a Try/Catch with a log file. But nothing writes to the log file.

I googled and googled, and checked all the following things:

  • I globally set Set-ExecutionPolicy to Unrestricted.
  • I tried running the task under a local account with admin permissions, as someone mentioned that domain accounts can’t run scheduled tasks. That doesn’t appear to be the case.
  • I checked that my account has Log on as batch.
  • I added the run script command to a batch file, and called the batch file from scheduled tasks. Still didn’t work!

It turns out that even when setting the Set-ExecutionPolicy to Unrestricted, you still need to add the parameter -ExecutionPolicy ByPass. Just that one little thing. If you are asking an application to run a script for you, it needs to bypass execution policy.

So the correct config was:

Program/script: “Powershell” (should resolve correctly to powershell.exe)

Arguments: -ExecutionPolicy ByPass -File {script file with path}

Start in: {Path of folder that script sits in}

Advertisements