r/PowerShell • • Oct 21 '26

Solved PowerShell Script and Task Scheduler 0x1 2147942401

I need a sanity check, as I feel like I am missing something obvious and I'm too close or too frustrated to see it.

I have a basic scheduled task. Runs as SYSTEM, no triggers, action to run a program, PowerShell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "C:\Tasks\MyScript.ps1"

I run this task it fails, 0x1 as last run result, event 201 action completed with return code from PowerShell of 2147942401

Things I have checked:

  • SYSTEM can access the script and directory it resides in
  • SYSTEM can execute other PowerShell scripts from task scheduler without issues
  • Tried using the full path to powershell.exe
  • Tried with and without quotes around the script path in -File parameter
  • With and without trailing '/' in the path for 'Start In'
  • Tried using -Command { } to create a file as a test
  • Removed all arguments except -File or -Command
  • Script is wrapped with Start/Stop-Transcript; never executes/no transcript is created

I don't know what else to try short of the old method of using a .bat to call PowerShell to see if that gives better clues as to what isn't working.

What am I missing that is preventing PowerShell from executing properly?

Edit: I knew it was something stupid and simple: there is a typo in -ExecutionPolicy (was spelled -ExcecutionPolicy). Found it when running the action arguments from PS as SYSTEM.

Thank you everyone for the assistance. I'm going to go sit in the corner with my dunce cap and contemplate the cobwebs.

9 Upvotes

42 comments sorted by

9

u/fdeyso Oct 21 '26

My guess is that cmdlets loaded in your profile are not loaded when it runs as system, so you have to import them at the beginning.

2

u/MRHousz Oct 21 '26

I wondered about that but the script is wrapped with Start/Stop transcript; it never gets executed.

11

u/Aggravating-Sock1098 Oct 21 '26

Download PsExec.

Then, in the command prompt, run:

psexec.exe -i -s cmd.exe

CMD is now running as SYSTEM.
Now execute the script and see what happens.

2

u/MRHousz Oct 21 '26

Will give this a try.

2

u/mitharas Oct 21 '26

Beware that psexec get's flagged by some av vendors.

1

u/MRHousz Oct 21 '26

Thanks for the reminder I'd have been yelling at Defender ASR rules lol

2

u/RikiWardOG Oct 21 '26

some? I feel like I've never had it not get flagged somewhere in the security chain.

1

u/OPhasballz Oct 21 '26

Sophos MDR called me after launching psexec from a notepad++ window against a bunch of hosts in AD xD

1

u/MRHousz Oct 22 '26

Ok, relaxed my ASR rule that blocks PSExec and gave it a try. PowerShell as SYSTEM executes the script with no issues. I have no forking idea why Task Scheduler is pitching a fit about it....

1

u/Aggravating-Sock1098 Oct 22 '26

Now try this:

psexec.exe -accepteula -nobanner -s powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "C:\pad\naar\test.ps1"

If it fails now, it means that your script must be executed interactively.

4

u/MRHousz Oct 22 '26

Yup, did that and found I had a typo that was the whole cause. Updated the post and have been in the corner with the dunce cap 🙃

1

u/Aggravating-Sock1098 Oct 22 '26

As a workaround, you could schedule your script using an administrative user account, launching it interactively as the SYSTEM user by prefixing the command with `psexec -i -s`.

4

u/fabrizob Oct 21 '26

Set the working directory explicitly. In the task's Start in (optional) field, enter C:\Tasks without quotes or a trailing backslash. Task Scheduler may otherwise start the process in C:\Windows\System32.

1

u/MRHousz Oct 22 '26

I did, with and without trailing '' and it still fails to execute.

1

u/RikiWardOG Oct 21 '26

This is my guess too

4

u/mitharas Oct 21 '26

PowerShell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "C:\Tasks\MyScript.ps1"

Just to be sure: You execute it in that order? -File needing to be the last command is a dumb quirk.

2

u/MRHousz Oct 21 '26

Yes, that is the order. Learned the hard way ages ago about -File needing to be last or it interpets any following as script params.

1

u/BlackV Oct 21 '26

Dunno if I'd call it a quirk, it's so you can pass arguments to the actual script (although they could have been nicer and made that a parameter too, save some pain)

3

u/Idakay Oct 21 '26

Add logging in each step and print the error, then run it and see what the actual problem is instead of guessing from what might be wrong

4

u/AmazingRealist Oct 21 '26

Or throw a Start/Stop-Transcript around the script

2

u/MRHousz Oct 21 '26

It never gets that far. The script has Start/Stop transcript wrapping it, it never is run.

6

u/purplemonkeymad Oct 21 '26

The error -2147942401 suggests 0x8007e001 which is basically a wrapper for application 0x1. When powershell runs successfully but encounters uncaught errors during runtime it outputs 0x1 as the exit code.

Basically this is a script level error rather than a task scheduler issue. I would temporarily trap and dump any errors to file:

# any requires headers here
trap {
    $_ | out-file errors.log -Append
}
# start of script here.

1

u/Common_Option_4385 Oct 22 '26

Do you have #Requires -RunAsAdministrator in your script? I got this error when I had accidentally edited this line, think I had removed the dash or something.

1

u/MRHousz Oct 22 '26

Negative, script has no #requires statements.

1

u/Hefty-Possibility625 Oct 22 '26

This is really dumb, but I noticed you are referencing powershell.exe instead of the full path. Use $PSHOME to find the full path to powershell and use the full path instead. That surprisingly fixed a lot of task scheduler issues when I had to use it.

1

u/MRHousz Oct 22 '26

I have tried both the full path to PowerShell and just PowerShell.exe. It fails to execute the script either way.

1

u/Hefty-Possibility625 Oct 22 '26

Another dumb thing to try is wrapping Bypass in quotes. It shouldn't be needed, but it could be a quirk of passing a string parameter with the command.

1

u/Majestic_Rhubarb_ Oct 21 '26

This is an access denied error.

Check permissions on the script file and any other profile files.

Is this your own computer, or corporate ?

2

u/MRHousz Oct 21 '26

Task runs as SYSTEM and I've confirmed that it has access to the folder and script file. Other tasks use scripts out of the same directory without issue.

1

u/Godcry55 Oct 22 '26

Any modules that are called by the script during runtime? If they aren’t accessible to SYSTEM that could be the issue.

1

u/MRHousz Oct 22 '26

Nothing special, just core PS functionality.

3

u/Majestic_Rhubarb_ Oct 21 '26 edited Oct 21 '26

Actually 2147942401 is 0x80070001, invalid function, not access denied.

Event 201 has an action element which should contain the actual command line executed.

You can redirect the output of the command line execution to a file using the standard operators in the arguments to the task.

Or as you suggest try a bat file wrapper to capture the output.

Does your path have a space in it ?

3

u/JSChronicles Oct 21 '26

2

u/BlackV Oct 21 '26

some top quality splatting in that post, I'm all for it

1

u/JSChronicles Oct 22 '26 edited Oct 22 '26

Well I wrote it a while back and deleted my account. Glad you still like it. Kinda rebranded myself and expanded into other languages. I kind of just pop in here and there

1

u/BlackV Oct 22 '26

Ah nice, whats your primary language these days ?

1

u/JSChronicles Oct 22 '26 edited Oct 26 '26

Jenkins (if you count that as a language), terraform (main language), python (main language), tiny bit of SQL. Other non-language, GitHub actions and workflow automations.

2

u/BlackV Oct 21 '26

Create a batch file, get your command working in that, confirm it works from a cmd prompt

Then run the cmd prompt as system, then run the same batch

Create the schedule task, point at batch

Now you have a system that is

  • Easily documented
  • Easily testable
  • Easily portable

My guess to your error is likely quoting or command line issue

1

u/Hefty-Possibility625 Oct 22 '26

I hated task scheduler so much that I build my own version of CRON using NSSM. I had a "manager" script running as a service that checked the scripts directory (including itself) for file changes. If it found a new file, it would run NSSM and create a new service for that file. If it found an updated file, it would restart the NSSM service. If a file was missing, it would remove the NSSM service.

All of that was easier than constantly dealing with Task Scheduler and all of the scheduling was inside the script itself. Never had to login to the damn script server again and managed the entire thing via repo and CI/CD.

-1

u/CookinTendies5864 Oct 21 '26

What is in arguments? Should be the file path.

run program: Powershell.exe
arguments: C:\Tasks\MyScript.ps1