banner



How To Schedule A Powershell Script In Windows Task Scheduler

PowerShell is 1 of the virtually popular scripting languages in the earth and is often used for automating tasks that are otherwise time consuming and effort-intensive. Built on the .NET Mutual Linguistic communication Runtime (CLR), you can find and filter all the data you need and use it to manage systems efficiently.

Every bit a PowerShell admin, you are likely to run unlike scripts at different times throughout the day for executing all the repetitive tasks. Many times, this can get overwhelming and there'south e'er a possibility of missing out on running a script at a particular time, and in plough, this could accept a serious bear on on your systems.

To avoid these hassles, you can schedule PowerShell scripts ahead of time, so they volition be executed automatically at the right time.

In this article, we will show you the dissimilar ways to schedule PowerShell scripts.

Use the task scheduler to schedule PowerShell scripts

Using the task scheduler is i of the easiest ways to schedule PowerShell scripts.

To do this:

  1. Right-click the Start push button and choose "Run"
  2. In the dialog box, blazon "taskschd.msc" and printing Enter. This opens the Task Scheduler. You can too navigate to it through the Administrative Tools choice in your Control Panel.
  3. In the Task Scheduler window, expect for the Task Scheduler Library on the left-hand pane.  Right-click information technology and from the menu, select "Create Job"

Yous will see many tabs in the task window, and so let's run through each of them in detail.

Full general tab

In the General tab, give a name to the task and give a clarification to make it piece of cake for others to identify its purpose later.

Look farther down and you'll see some security options. First off, decide if the user must exist logged in to run this task. Enabling this option helps you to rails who has started a task and gives a log of user admission.

The other security choice is to decide if a task requires the highest privileges. Again, this is a part of user access command.

Trigger tab

The trigger tab will be empty when yous open it for the first fourth dimension.

Click the "New" button at the bottom to create a new trigger. In the new window, cull the frequency, the start date, the end date, and more.

An of import option is the "Begin the task:" option. Hither. cull if you want it to run on a schedule, at the time of startup, or on whatsoever other predetermined issue trigger. Merely cull from the driblet-down box.

There are as well a few other advanced settings such equally delays, repeats, expiry, and more. Choose the options based on your needs.

Deportment tab

The Actions tab will also be empty when y'all open it for the showtime fourth dimension. Click the "New" push. Choose "powershell.exe" for the Program/Script option.

You also have other options and tin can make up one's mind when the programme must offset, where information technology must offset, and any optional arguments as required. In the "Add Arguments" option, choose your script.

Weather condition and Settings tab

The Weather condition 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 job is at present scheduled. To view or change your task'due south settings, open the chore director, navigate to the task, right-click, and modify or view.

Utilize PowerShell

The 2nd option is to use PowerShell cmdlets to schedule these scripts.

This is a good alternative to using a GUI, particularly 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 commencement step, decide the start time and frequency of a task. For example, you lot tin can schedule a task to start at ix AM every day.

To set this, utilize the New-ScheduledTaskTrigger cmdlet. The lawmaking for the above schedule is,

 $scheduledTime=New-ScheduledTaskTrigger -At ix.00AM -Daily

This cmdlet has many parameters such every bit,

  •    [-RandomDelay <TimeSpan>]
  •    -At <DateTime>
  •    [-Once]
  •    [-RepetitionDuration <TimeSpan>]
  •    [-RepetitionInterval <TimeSpan>]
  •    [-CimSession <CimSession[]>]
  •    [-ThrottleLimit <Int32>]
  •    [-AsJob]
  •    [<CommonParameters>]

Use these parameters to schedule a chore but the way you want.

Set the actions

New-ScheduledTaskAction is the cmdlet for setting the deportment of a chore, and y'all can schedule up to 32 tasks with information technology. Withal, the tasks will execute one after another and there is no parallel execution.

Employ this cmdlet to create new tasks.

For example,

New-ScheduledTaskAction -Execute "PowerShell.exe"

The above code opens PowerShell.exe

$scheduledAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -WorkingDirectory C:/MyScripts

This code volition open PowerShell and will change the directory to MyScripts. Equally you can see, the tasks are executed sequentially.

Save the scheduler

Now that y'all accept divers the task and the time when information technology should execute, the final step is to save the schedule on your local organisation, and so you can reuse or edit information technology later. Utilize the cmdlet Annals-ScheduledTask to save your schedule.

Though your task can execute any file type, note that this cmdlet doesn't cheque for compatibility with your operating system.

Register-ScheduledTask -TaskName "ExecuteMyScript" -Trigger $scheduledTime -Activity $scheduledAction

This job will open PowerShell and change the directory to "MyScripts" at ix AM every day.

Thus, this is another manner to schedule a PowerShell script.

Boosted actions

Let's now take a look at some boosted actions that you lot may want to use such equally backups and deletions.

Back up

In PowerShell, in that location is no built-in cmdlet for backing up a scheduled task, but since everything is treated as an object, you can always motion the object to a file for backup.

Go-ScheduledTask -TaskName 'ExecuteMyScript' | Export-Clixml c:\MyScripts.xml

In the higher up example, you lot're sending a task to an XML file for backup.

Delete a task

To delete a scheduled job,

Unregister-ScheduledTask -TaskName 'ExecuteMyScript' -Confirm:$true

This cmdlet will delete your chore after yous confirm. This bank check ensures that you don't accidentally delete a scheduled job.

To conclude, PowerShell scripts are a preferred way to implement automation because they are uncomplicated to programme and it comes with cmdlets to do well-nigh anything you need.

You can even instruct the organization 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.

Source: https://techgenix.com/how-to-schedule-powershell-scripts/

Posted by: moranaboughtters.blogspot.com

0 Response to "How To Schedule A Powershell Script In Windows Task Scheduler"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel