In this post, we are going to see how to create a scheduled task using PowerShell. We will look into three important use cases. The first use case is to run the scheduled task every day at a certain time, the second case is to run the task on a specific trigger, and the final use case is to run the task periodically every minute.
We use PowerShell for the following reasons:
- To execute some instructions and get the output.
- Write Windows .NET programs without having to compile code.
- Create complicated programs on the go.
The above reasons are enough to reuse the written script over and over again. But wouldn’t it be more convenient if we can automate the process of using the script again and again when we want. Here is where Scheduled tasks come in.
The reasons for using Scheduled tasks are:
- Execute the PowerShell script regularly without the user prompt.
- Automate script execution.
- Streamline the user workflow.
Now that we have seen the important details that we need to know before using PowerShell scheduled tasks, let us see how to create Scheduled tasks using PowerShell.
- PowerShell Scheduled tasks that runs every day.
- A scheduled task that runs periodically.
- A scheduled task that runs at Logon.
People also search for:
PowerShell Scheduled task that run Every Day:
- Run PowerShell Scheduled-task every day at a specified time.
- PowerShell Scheduled task daily.
- Register-ScheduledTask and run on a daily basis.
To begin with, we will create a basic scheduled task that runs every day at a specified time. This task will be scheduled to execute at 9 pm every day. Thus the user will not have to prompt the script every time at the specified time.
The Scheduled Task is created in three parts.
- The first part is action, here we specify what needs to be done.
- The second part is the trigger, in the trigger, we have to specify the condition when the task is executed.
- Finally, the third part is the most important part, here we register the scheduled task in the task scheduler. Keep in mind that only the registered tasks are executed.
For the action, we are choosing the script that moves the files created on the same day to the same folder. The script is as shown below.
Do not forget to change the path while executing it by yourself. We can use the above script and build the script needed for registering the scheduled task. The above given script will go inside the action block of the scheduled task.
The above script represents the action for the scheduled task.
We will create a trigger such that the action will be performed at a specified time.
Here we can see that the action must be performed at 5 PM.
Finally, we will use the $action variable and $trigger variable and register it in the task scheduler with the appropriate name and description.
We have the necessary script with us but we have not used the script yet. To use the script follow the below instruction.
- Press the start button.
- Enter PowerShell.
- Right-click and select, run as administrator.
- It will open PowerShell with administrator privileges.
- We have to enter the script in PowerShell.
We can see the scheduled task in the TaskScheduler. It is shown in the image.
PowerShell Schedule task that runs Periodically:
- Run Powershell scheduled tasks again and again.
- Run PowerShell scheduled task after time interval.
- PowerShell Scheduled task -RepetitionInterval example
To run the PowerShell scheduled task periodically, we need to carefully create the triggers.
- Carefully create the triggers.
- Register it in the Task Scheduler.
For our example, we will use the same action block used earlier. But we will use a new trigger definition with a new trigger name. The trigger is given below.
- The trigger will start after a random delay of 0 seconds.
- The trigger will start on 30th Jan 2020 and today we are well past that.
- Once started, it will run once.
- The repetition duration is 24 hours, which means, after 24 hours, the task will no longer repeat.
- The throttle limit set to 1 tells windows that it should run only 1 task at a time. This prevents a new task from starting if the previous task is not over executing.
We will register the new scheduled task using the below script.
We can see the scheduled task in the Task Scheduler.
PowerShell Scheduled task at Logon:
- How to run PowerShell task at logon.
- PowerShell tasks New-ScheduledTaskTrigger -atlogon
Just like running the Scheduled task every minute, we need to create a trigger that will make the scheduled task run at logon.
The script for triggering the scheduled task at logon is very simple and it is given below.
We can modify the register scheduled task and register the task.
The registered scheduled task is shown below.
In this post, we learned how to create a scheduled task that can run at a specified time, run periodically, and run at logon. If you want to know more about PowerShell Scheduled task, please let us know in the comments below.