Powershell: pwsh.exe executable does not work in Windows Task Scheduler

Created on 29 Jul 2020  路  9Comments  路  Source: PowerShell/PowerShell

Using pwsh.exe as the executable in windows task scheduler returns: The system cannot find the file specified. (0x8007002) although pwsh.exe works throughout the system.

Expected bahavior: pwsh.exe works as an executable like powershell.exe does

Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.19041
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Issue-Question Resolution-Answered

Most helpful comment

Update:
After hours on this I came across this: https://superuser.com/questions/331077/accessing-environment-variables-in-a-scheduled-task

Apparently you have to restart windows for task scheduler to recognize new system variables. This machine has not restarted since installing PS7. A simple restart and everything now works as expected. Who would have thought.

Maybe it's because task scheduler has not been updated since Vista, 13-1/2 years ago: https://en.wikipedia.org/wiki/Windows_Task_Scheduler#Task_Scheduler_2.0

I am grateful to both @doctordns and @vexx32 for taking the time to respond and test... thank you!

Super User
When setting up a scheduled task, for the action section I'm trying to access an environment variable for the location of the executable that I wish to run. This is mainly because on one machine the

All 9 comments

If the Task Scheduler cannot find pwsh.exe, there is an issue with your PATH environment variable.

I would guess that the home folder of pwsh.exe is on the user version of the PATH variable, but not the machine version of the PATH variable, and you'll need to add it to the machine PATH for it to be recognised in Task Scheduler.

I just tested this here and it works as expected. I set up a task to run pwsh.exe with a simple script to test it and it runs as expected.

It was actually already in the system variables but not the user variables so I added it but it still fails. What I've tried:

  1. Having the Path in both user & system variables
  2. Having the Path only in user variables
  3. Having the Path only in system variables
  4. Creating a new task in task scheduler
  5. Tried both pswh & pswh.exe in task scheduler

However, in cmd both pwsh & pwsh.exe work so I know the Path is there.
The absolute path for PowerShell 7 works in task scheduler
powershell.exe works in task scheduler

This doesn't make any sense

That sounds like it might be an issue with Task Scheduler at the moment, I'm not really sure what troubleshooting steps you can go to from there. 馃槙

I've tested the task scheduler with PWSH 7 (for a book contents) and this does not happen on my workstation or test VMs, running a scheduled task that runs a script works as expected.

Which versions of PowerShell 7 are you running? RTW, Preview??

Can you use event viewer to take a look at the task scheduler's event logs?? Take a look at the Applications and Services Logs\Microsoft\Windows\Task Scheduler\operational log. It should show the definition, start-up and completion of the task. I have just re-tested this - and I can see all these events. Also, if I create a task with a 'broken' task (eg I run pwsh7.exe which of course does not exist) I get error entries in the log, as expected.

I suggest you remove the task, clear the logs, then re-add the task and run it immediately. Then re-look at the event log.

Created a new task. Log just show Task Scheduler failed to launch action "pwsh.exe". Tried to run with the highest privileges also.
Since the PS7 path works in cmd and task scheduler can see other paths with no issues, it must be something specific with PS7 and task scheduler together on this machine as it's works on your machine.

Update:
After hours on this I came across this: https://superuser.com/questions/331077/accessing-environment-variables-in-a-scheduled-task

Apparently you have to restart windows for task scheduler to recognize new system variables. This machine has not restarted since installing PS7. A simple restart and everything now works as expected. Who would have thought.

Maybe it's because task scheduler has not been updated since Vista, 13-1/2 years ago: https://en.wikipedia.org/wiki/Windows_Task_Scheduler#Task_Scheduler_2.0

I am grateful to both @doctordns and @vexx32 for taking the time to respond and test... thank you!

Super User
When setting up a scheduled task, for the action section I'm trying to access an environment variable for the location of the executable that I wish to run. This is mainly because on one machine the

Oh, huh! I guess that makes some sense, glad you got it sorted!

For some reason I was thinking that task scheduler would pick it up anyway, but that's good to know!

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings