PowerShell is a powerful and the most popular scripting language used in Windows environments.
It helps us automate most of our routine tasks and saves us a lot of time and headaches. These scripts do everything from monitoring disk health to reclaiming resources from virtual machines that you no longer use.
Engineers and Admins usually spend a lot of time running all those scripts manually and thereafter analyzing the results. This is where the Windows Task Scheduler comes into the picture.
The Windows Task Scheduler comes pre-installed in all recent Windows operating systems. You can use this tool to schedule any application to run at a predetermined time or day and on a repeated schedule or only, one-time.
In this tutorial, we’ll show you how to schedule Powershell scripts using the Windows task scheduler or via another Powershell script.
Schedule PowerShell Script Using Windows Task Scheduler
In this section, we will learn how to create and schedule a task using the Windows task scheduler.
Follow the below steps to create and schedule a task using the Windows task scheduler:
Step 1 – Press Windows + R (or click the start button and type run ) and type taskschd.msc to launch the Windows Task Scheduler as shown below:
Step 2 – Click on the Create a task to create a new task as shown below:
Step 3 – In the General tab, provide your task name, description and Select “ Run Whether the user is logged on not“.
You can also choose another option if you want to run the task interactively.
Then, click on the Triggers tab as shown below:
Step 4 – Here, we’ll need to specify the conditions that will trigger the task.
Click on the New button to define the conditions.
You should see the following page:
Step 5 – Select “ On an event“.
Select Application in Log box and type id in Event ID box. Then, click on the OK button.
Step 6 – In the Actions tab, you will need to specify the action that will occur when your task starts as shown below:
Step 7 – Click on the New button to create a new action as shown below:
Step 8 – Provide your desired action as shown below:
Action: Start a program
Add arguments (optional): – The file path to the script.
Next, click on the OK button.
Step 9 – Click on the Conditions tab as shown below:
Step 10 – Specify your desired conditions and click on the OK button.
Step 11 – Click on the Settings tab as shown below:
Step 12 – Specify additional settings for your task and click on the OK button.
You will be asked to provide your Administrator username and password as shown below:
Step 13 – Provide your username, password and click on the OK button to create and save the task successfully.
The above schedule script will run automatically at your specified time.
Schedule PowerShell Script with PowerShell Scripts
In this section, we’ll show you how to create and schedule a task using PowerShell script.
We will create a task that runs daily at 2 AM.
This task executes the PowerShell script that will monitor changes to group membership in the Active Directory site.
Open your Windows Powershell and run the following script to create and scheduled task:
$Trigger= New-ScheduledTaskTrigger -At 2:00am –Daily
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\PS\StartupScript.ps1"
Register-ScheduledTask -TaskName "MonitorGroupMembership-Windows" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
You should see the following output:
You can also use some additional options in the above scripts:
- -AtStartup – This will triggers your task at Windows startup.
- -AtLogon – This will triggers your task when the user signs in.
- -Once – This will triggers your task once.
- -Weekly –This will triggers your task once a week.
Compared to the Task Scheduler tool, the Powershell script will not allow triggering execution “on an event”. So this one of the disadvantages of scheduling a task with PowerShell.
In the above guide, we’ve learned how to Create and Schedule tasks using the Windows task scheduler and using a Powershell script. We hope you can now automate your day-to-day tasks easily with the task scheduler tool.