PowerShell script won’t execute as a Windows scheduled task

If the problem you’re having is with Execution Policy, then you can also set the execution policy of a specific invocation of PowerShell. This is what I usually do when executing PowerShell through a scheduled task:

powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File \\path\to\script.ps1

Why?

-NoProfile

This ensures that you don’t rely on anything in the user’s PowerShell profile, and avoids the overhead of executing that additional code.

-NoLogo

This mostly doesn’t matter; maybe it does if you’re capturing the output of your script. Mostly it makes me feel better.

-NonInteractive

Ensures that your task won’t wait indefinitely if something in your script unexpectedly prompts the user. With this switch, the script will just exit instead; at least you’ll have an error code instead of a hanging script.

-ExecutionPolicy Bypass

You can use Unrestricted here or whichever execution policy you like. This is probably the one you need the most.

Why I prefer setting Execution Policy this way:

Because I don’t want the task to depend on a global non-default setting that you may have other reasons to change in the future. If some other process depends on a different execution policy, then it’s not at odds with your task this way.

Plus it’s always nice not to have to change the defaults. Less to remember/document/test.

Bonus

See JohnLBevan’s answer for some additional causes of 0x1 result in a scheduled task.

Leave a Comment