Friday, April 30, 2021

How to Fix Error 0x1 in Windows Task Scheduler when Running a PowerShell Script

Recently I wanted to configure a simple script to send an email notification any time a particular Windows 10 machine rebooted. After some quick googling, I whipped up a PowerShell script and then set it up to run in Windows Task Scheduler whenever the system starts up.

But, running a PowerShell script as a scheduled task in Windows isn't so simple. First, you need to invoke the simple PowerShell executable (easy to do as powershell.exe is in the Windows path already), and second, you need to specify what PowerShell script to run using the -File argument. After configuring both of these, I still could not figure out why it was throwing the error 0x1 when trying to run the PowerShell script. Apparently, this is due to a PowerShell execution policy that prevents the script from running (for security reasons). Thankfully, Stack Overflow user briantist posted a nice solution to this issue. All you need to do is configure the task with -ExecutionPolicy Bypass in the arguments when setting up the task:

 

Don't forget to also add -File "C:\Path To Script\MyScript.ps1" to your arguments to reference the actual script when invoking PowerShell in your task.

It took me days to figure out why I couldn't get this task to run, but thankfully, Stack Overflow came through, and I was able to get it to run. I hope this helps someone else out there who may be getting the 0x1 error when trying to run a PowerShell script as a Scheduled Task in Windows.


No comments:

Post a Comment