1
votes

I have a simple batch file which calls a powershell script.

I've looked at the following 3 previous questions on the subject as well:

Run a batch file from Task Scheduler is not working with a java command

Windows Task Scheduler doesn't start batch file task

Task Scheduler not executing batch (bat) file with MSTest commands

It seems like I've tried every single combination of running the task and it still doesn't execute my powershell script.

batch file contents: powershell.exe "E:\SIS\fileCopy.ps1"

If I run the command in the batch file manually, it runs just fine. Here are things I've tried to do to get it working:

  • I've change the Security options to run as System with "Run with highest priveleges" checked
  • I've tried every other combination of "Run only when user is logged on", "Run whether user is logged on or not" and "Run with highest priveleges"
  • I've tried adding the "Start in (optional" setting to the folder where the files are located.

I'm at my wits end and can't believe Microsoft hasn't figured out a way to make this easier.

3
Why run it as a batch file? Why not have Task Scheduler run powershell directly?TheMadTechnician
I honestly don't care how I run it but I was told this was the way to schedule powershell scripts. Do you have a better way of scheduling powershell scripts?Geoff Dawdy

3 Answers

1
votes

You need to have task scheduler execute Powershell.exe and have the arguments be the path to your .ps1 file.

To validate your script is running properly, you should set the Security options to 'Run only when user is logged on'. It will then pop the powershell dialog when it runs. I often also use start-transcript to view the results of scheduled poweshell scripts.

After you validate the script is running correctly, you can set the security options however best fits your situation.

0
votes

Use the execution policy flag to flag that instance to unrestrisicted because your powershell settings may be blocking script execution. powershell -executionpolicy unrestricted -Command "E:\SIS\fileCopy.ps1"

0
votes

I found that Task Scheduler can't run a batch file if it lives in a folder that is being synced by OneDrive. I had to move the batch file to another folder to get Task Scheduler to be able to run it.