📎 AI Summary:
The thread discusses issues with running scripts via Windows Task Scheduler, where the scripts work manually but fail to execute properly when scheduled. The original poster identifies that the problem relates to working directory context and script execution policies, and finds a solution by specifying the working directory in PowerShell’s Start-Process command and bypassing execution policies in the scheduled task. Overall, the contributors share troubleshooting steps and effective workarounds to ensure scripts run smoothly through task scheduling.

erotavlas

Well-Known Member
Member details
Joined
Sep 17, 2018
Messages
3
Thread Author #1
Hi,
I'm new of this thread and I do not know if this is the right section of the forum. I'm under windows 10 1803 64 bit.
I have two scripts (.cmd and .ps1) which execute the same operation. The scripts work well if I run them from cmd or powershell while they do not work if I use schtasks.
In particular, the job is created correctly and it starts after login, but the scripts fail to execute. The script executes a program in a new windows, but it is closed immediately and it is not possible to catch the error.
I tried to redirect the output of cmd in order to capture the error, but it does not work.
Code:
start cmd /c program arg1 arg2 2^> out_err.txt
Do you have any suggestion?
 

Last edited:
Solution
I solved in this way:
Code:
Start-Process -WorkingDirectory $path -FilePath "$path\program" -ArgumentList "arg1 arg2" -WindowStyle Maximize

Schtsk:
Code:
PowerShell -ExecutionPolicy Bypass -NoProfile -InputFormat None -File C:\path\script.ps1

Neemobeer

Windows Forum Team
Staff member
Member details
Joined
Jul 4, 2015
Messages
8,995
Did you try adding the word PAUSE or change it to cmd /K. /C will terminate the command prompt.
 

erotavlas

Well-Known Member
Member details
Joined
Sep 17, 2018
Messages
3
Thread Author #3
Thank you very much. Your workaround worked, by using cmd /k I found that the problem was related to the working directory.
Both the following works well.
Code:
start /D %scriptPath% %scritpPath%program arg1 arg2
Code:
start /D %scriptPath% cmd /c %scriptPath%program arg1 arg2

I would like to get the same result via powershell:
Code:
Start-Process -WorkingDirectory $path -FilePath "$path\program" -ArgumentList "arg1 arg2" -WindowStyle Maximized

However, in this case adding the path does not change the effect.
 

Neemobeer

Windows Forum Team
Staff member
Member details
Joined
Jul 4, 2015
Messages
8,995
Use the argument field available in task scheduler, also do you have your execution policy set to allow scripts?
 

erotavlas

Well-Known Member
Member details
Joined
Sep 17, 2018
Messages
3
Thread Author #5
I solved in this way:
Code:
Start-Process -WorkingDirectory $path -FilePath "$path\program" -ArgumentList "arg1 arg2" -WindowStyle Maximize

Schtsk:
Code:
PowerShell -ExecutionPolicy Bypass -NoProfile -InputFormat None -File C:\path\script.ps1
 

Solution