PowerShell Script Scheduling using Task Scheduler Trcik

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

 

PowerShell Scheduling 1

 

  •  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

PowerShell Scheduling 2

 

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 me@contoso.com -EmailFrom admin@contoso.com -UseSecure:$false  ….. . This becomes even longer with some complex scripts.

In Summary, you cannot type long strings here and expect everything to work fine.

Solution ?

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 me@contoso.com -EmailFrom admin@contoso.com -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.

PowerShell Scheduling 3

 

PowerShell Scheduling 4

 

See also my previous post for scheduling Exchange PowerShell Scripts 

3 comments on “PowerShell Script Scheduling using Task Scheduler Trcik

  1. Pingback: Exchange Dashboard Organization – “Email Report” PowerShell Script | Ammar Hasayen - Blog

  2. Pingback: How to run PowerShell Scripts efficiently from Task Scheduler? | Ammar Hasayen - Blog

  3. Pingback: How to Create a PowerShell Script as Scheduled Task | Aslam's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s