Automating PowerShell Scripts Using Task Scheduler

  1. Running PowerShell Scripts Using Task Scheduler
  2. Creating Scheduled Tasks in PowerShell
Automating PowerShell Scripts Using Task Scheduler

Windows Task Scheduler can help us automatically launch a program or PowerShell script at a specific time or when certain conditions and triggers are met.

This article will show us how to run a PowerShell script from Task Scheduler and create scheduled tasks using Windows PowerShell.

Running PowerShell Scripts Using Task Scheduler

Open the Task Scheduler console by pressing Windows+R and then typing taskschd.msc in the opening window. Once executed, follow the steps below.

First, click on Create a task and enter a name and description for the new task. Next, check the Run with the highest privileges box to run the program with administrator privileges.

When running tasks, it is best to assign a separate service account that will execute the task whether the user is currently in session or not.

Creating a Task

Second, click on the Triggers tab and click the New… button. Here we can specify the conditions that trigger the task to be executed.

There are multiple ways to begin a task.

  • On a Schedule
  • At log on
  • At startup
  • On idle
  • On an event
  • At task creation/modification
  • On connection to a user session
  • On disconnection from a user session
  • On workstation lock
  • On workstation unlock

Pick a trigger that will fit the situation.

Task Triggers

Third, click on the Actions tab, and click New…. Here we can specify the actions that will be executed whenever the trigger conditions are met.

This step must be followed exactly to properly run PowerShell. To automatically schedule the PowerShell script that the system will execute, fill in the following fields:

  • Action: Start a program
  • Program\script: powershell (or powershell.exe)
  • Add arguments (optional): -File (Specify the path of the file here) (Any other arguments are placed here as well)

Click on OK to apply the changes.

Task Actions

Fourth, the Conditions tab enables us to specify the conditions that, along with the trigger, determine how the system should run the task.

Task Conditions

Fifth, we can also set up additional parameters for your scheduled task on the Settings tab.

Task Settings

Sixth, when the task is configured, the system will ask us for the password of the account provided in the first step. Enter the password and hit OK to save the task.

Once that is done, the task should run the PowerShell file successfully if the system has initiated it through the setup trigger.

Creating Scheduled Tasks in PowerShell

Now that we have discussed setting up a task using Task Scheduler, let’s discuss how to create a scheduled task using PowerShell.

PowerShell 3.0 and 4.0 introduced cmdlets for creating scheduled tasks, like New-ScheduledTaskTrigger and Register-ScheduledTask.

# Specify the trigger settings
$Trigger = New-ScheduledTaskTrigger -At 10:00am -Daily

# Specify the account to run the script
$User = "NT AUTHORITY\SYSTEM" 

# Specify program to run (PowerShell)
$Action = New-ScheduledTaskAction -Execute "PowerShell" -Argument "-file C:\PS\PSScript.ps1" 

# Specify the name of the task
Register-ScheduledTask -TaskName "Test Script" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force

Other trigger choices that could be useful in setting up new tasks include:

  • The -AtStartup triggers our task at Windows startup.
  • The -AtLogon triggers our task when the user signs in.
  • The -Once triggers our task once. We can set a repetition interval using the -RepetitionInterval parameter.
  • The -Weekly triggers our task once a week.

Note that, when using these cmdlets, it is impossible to trigger execution on an event as we did with the Task Scheduler tool.

PowerShell scripts with on an event triggers are much more complicated, so this is a disadvantage of using PowerShell rather than Task Scheduler.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell Task Scheduler