PowerShell is one of the most popular scripting languages in the world and is often used for automating tasks that are otherwise time consuming and effort-intensive. Built on the .NET Common Language Runtime (CLR), you can find and filter all the information you need and use it to manage systems efficiently.
As a PowerShell admin, you are likely to run different scripts at different times throughout the day for executing all the repetitive tasks. Many times, this can get overwhelming and there’s always a possibility of missing out on running a script at a particular time, and in turn, this could have a serious impact on your systems.
To avoid these hassles, you can schedule PowerShell scripts ahead of time, so they will be executed automatically at the right time.
In this article, we will show you the different ways to schedule PowerShell scripts.
Use the task scheduler to schedule PowerShell scripts
Using the task scheduler is one of the easiest ways to schedule PowerShell scripts.
To do this:
- Right-click the Start button and choose “Run”
- In the dialog box, type “taskschd.msc” and press Enter. This opens the Task Scheduler. You can also navigate to it through the Administrative Tools option in your Control Panel.
- In the Task Scheduler window, look for the Task Scheduler Library on the left-hand pane. Right-click it and from the menu, select “Create Task”
You will see many tabs in the task window, so let’s run through each of them in detail.
In the General tab, give a name to the task and give a description to make it easy for others to identify its purpose later.
Look further down and you’ll see some security options. First off, decide if the user must be logged in to run this task. Enabling this option helps you to track who has started a task and gives a log of user access.
The other security option is to decide if a task requires the highest privileges. Again, this is a part of user access control.
The trigger tab will be empty when you open it for the first time.
Click the “New” button at the bottom to create a new trigger. In the new window, choose the frequency, the start date, the end date, and more.
An important option is the “Begin the task:” option. Here. choose if you want it to run on a schedule, at the time of startup, or on any other predetermined event trigger. Simply choose from the drop-down box.
There are also a few other advanced settings such as delays, repeats, expiry, and more. Choose the options based on your needs.
The Actions tab will also be empty when you open it for the first time. Click the “New” button. Choose “powershell.exe” for the Program/Script option.
You also have other options and can decide when the program must start, where it must start, and any optional arguments as required. In the “Add Arguments” option, choose your script.
Conditions and Settings tab
The Conditions and Settings tabs contain a bunch of options related to starting and stopping tasks and error handling. Look through these options and choose the ones you need.
Click OK and your task is now scheduled. To view or change your task’s settings, open the task manager, navigate to the task, right-click, and change or view.
The second option is to use PowerShell cmdlets to schedule these scripts.
This is a good alternative to using a GUI, especially if you’re comfortable using PowerShell.
Scheduling PowerShell scripts using cmdlets has three distinct steps, and they are explained below.
Set the time
As a first step, decide the start time and frequency of a task. For example, you can schedule a task to start at 9 AM every day.
To set this, use the New-ScheduledTaskTrigger cmdlet. The code for the above schedule is,
$scheduledTime=New-ScheduledTaskTrigger -At 9.00AM -Daily
This cmdlet has many parameters such as,
- [-RandomDelay <TimeSpan>]
- -At <DateTime>
- [-RepetitionDuration <TimeSpan>]
- [-RepetitionInterval <TimeSpan>]
- [-CimSession <CimSession>]
- [-ThrottleLimit <Int32>]
Use these parameters to schedule a task just the way you want.
Set the actions
New-ScheduledTaskAction is the cmdlet for setting the actions of a task, and you can schedule up to 32 tasks with it. However, the tasks will execute one after another and there is no parallel execution.
Use this cmdlet to create new tasks.
New-ScheduledTaskAction -Execute “PowerShell.exe”
The above code opens PowerShell.exe
$scheduledAction = New-ScheduledTaskAction -Execute “PowerShell.exe” -WorkingDirectory C:/MyScripts
This code will open PowerShell and will change the directory to MyScripts. As you can see, the tasks are executed sequentially.
Save the scheduler
Now that you have defined the task and the time when it should execute, the final step is to save the schedule on your local system, so you can reuse or edit it later. Use the cmdlet Register-ScheduledTask to save your schedule.
Though your task can execute any file type, note that this cmdlet doesn’t check for compatibility with your operating system.
Register-ScheduledTask -TaskName “ExecuteMyScript” -Trigger $scheduledTime -Action $scheduledAction
This task will open PowerShell and change the directory to “MyScripts” at 9 AM every day.
Thus, this is another way to schedule a PowerShell script.
Let’s now take a look at some additional actions that you may want to use such as backups and deletions.
In PowerShell, there is no built-in cmdlet for backing up a scheduled task, but since everything is treated as an object, you can always move the object to a file for backup.
Get-ScheduledTask -TaskName ‘ExecuteMyScript’ | Export-Clixml c:\MyScripts.xml
In the above example, you’re sending a task to an XML file for backup.
Delete a task
To delete a scheduled task,
Unregister-ScheduledTask -TaskName ‘ExecuteMyScript’ -Confirm:$true
This cmdlet will delete your task after you confirm. This check ensures that you don’t accidentally delete a scheduled task.
To conclude, PowerShell scripts are a preferred way to implement automation because they are simple to program and it comes with cmdlets to do almost anything you need.
You can even instruct the system to automatically execute PowerShell scripts at specified times using either a GUI like a task scheduler or through Powershell cmdlets, depending on your personal preferences.
Which of the two methods do you prefer and why? Please share your thoughts in the comments section.