I have a PowerShell script (that works). In Windows Task Scheduler I created a new task to execute \"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\"
There are several possible causes for a PowerShell script invoked by the task scheduler to complete with code 0x1
:
Run with highest privileges
flag (checkbox on the task's General tab) enabled.*-File ".\MyScript.ps1" -Parameter1 'Demo'
, instead try: -Command "& .\MyScript.ps1 -Parameter1 'Demo'"
*As Ben points out in the comments, Run with highest privileges
doesn't have to be enabled / it depends if the script requires admin rights (e.g. if some commands like Set-ExecutionPolicy
or Stop-Process
may require these). If you're not sure, try ticking the option to see if it fixes your issue; if it doesn't seem to make a difference, leave it unchecked.
If you don't have any error messages and don't know what the problem is - why PowerShell scripts don't want to start from a Scheduled Task, do the following steps to get the answer:
You should be able to see all error notifications.
In case of one of my script it was:
Unable to find type [System.ServiceProcess.ServiceController]. Make sure that the assembly that contains this type is loaded.
And in this case I have to add an additional line at the beginning of the script to load the missing assembly:
Add-Type -AssemblyName "System.ServiceProcess"
And the next errors:
Exception calling "GetServices" with "1" argument(s): "Cannot open Service Control Manager on computer ''. This operation might require other privileges."
select : The property cannot be processed because the property "Database Name" already exists
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
This ensures that you don't rely on anything in the user's PowerShell profile, and avoids the overhead of executing that additional code.
This mostly doesn't matter; maybe it does if you're capturing the output of your script. Mostly it makes me feel better.
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.
You can use Unrestricted
here or whichever execution policy you like. This is probably the one you need the most.
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.
See JohnLBevan's answer for some additional causes of 0x1 result in a scheduled task.
I've done this before and had similar issues. It's almost always the PowerShell security settings. Most obviously, I would double-check your execution policy (assuming that you've set it).
Which user does the task run as? Has that user run a PowerShell script before? If I remember right, each user is prompted to "allow" PowerShell scripts to run (Y/N), when running a script for the first time (regardless of execution policy). That has bitten me before. Try:
After the first run, you shouldn't have to worry about that again, and it should run from the task scheduler just fine.
Depending on your domain security, you might also have to set the group execution policy. Here's an article that details how to do that, as well as a couple other things to check: PowerShell Security.
The previous answers are of great value. I added the below value in the Add Arguments field.
-noninteractive -nologo -command "&{path\to\script.ps1}"
Make sure to add the ampersand and enclose the path in curly braces. Don't forget to have double quotes before ampersand and after closing curly braces.