In this post, I want to talk about my personal experience scheduling PowerShell script using Windows Task Scheduler.
Let us start with Scheduling simple PowerShell Script called MyScript.PS1 located on a directory F:\Scripts\
- Go to Task Scheduler in Windows> Create Basic Task, give it a name
- In the Actions section, click (Start a Program):
- In the Program/Script , Type C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- In the Add arguments (Optional), type F:\Scripts\MyScript.PS1
So what can get wrong?
In the previous example, the Add Arguments (Optional) section, will not behave correctly when you type long data on it.
Suppose that your script takes many parameters, so on the Add Arguments (Optional) field, you will type F:\Scripts\MyScript.PS1 -SendEmail:$true -From email@example.com -EmailFrom firstname.lastname@example.org -UseSecure:$false ….. . This becomes even longer with some complex scripts.
In Summary, you cannot type long strings here and expect everything to work fine.
So the best thing to do, is to open a new PowerShell script file, type on it the full command that you want to run , in our case F:\Scripts\MyScript.PS1 -SendEmail:$true -From email@example.com -EmailFrom firstname.lastname@example.org -UseSecure:$false, save it on the same directory as the original script F:\Scripts with any name like “RunMe.PS1″, and create one schedule task and type F:\Scripts\Runme.PS1 on the Add Arguments (Optional) field.
See also my previous post for scheduling Exchange PowerShell Scripts