How to Create Scheduled Task Using PowerShell

The task list for a majority of system administrators includes writing various scripts and launching them. Scheduling the launch of the script using the graphical interface of the Task Scheduler console is a quite simple task. In this small article we’ll show you a new way to create scheduled task using the PowerShell console.

Suppose you need to create a task that will run the PowerShell script every Tuesday and Thursday at 10:00 PM.

In order to create a new scheduled job on Windows 7 and older versions you must have at least PowerShell 3.0 installed. At first, you need to run elevated PowerShell command prompt and import PowerShell module named PSScheduledJob.

Import-Module PSScheduledJob

Now let’s create a new job trigger:

$Trigger = New-JobTrigger -Weekly -DaysOfWeek 2,4 -At 10:00PM

After that create a new task named BackupDBTask and bind the created trigger to it:

Register-ScheduledJob -Name BackupDBTask -FilePath "C:\ps\backupdb.ps1" -Trigger $Trigger

powershell scheduled task

As you can see, the new scheduled task was successfully created. You can find it using Task Scheduler GUI in the following section: Task Scheduler -> Task Scheduler Library -> Microsoft ->Windows -> PowerShell -> SheduledJobs.

powershell create scheduled task

When creating a new job, PowerShell additionally generates an xml file with a scheduled task definition. You can find this file in the folder:

%USERPROFILE%\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\BackupDBTask

You can use this xml file to import task you created to other computers or servers:

Register-ScheduledTask -Xml (Get-Content '\\server1\ScheduledJobDefinition.xml' | out-string) -TaskName "BackupDBTask"

The Output folder will contain the history of the job run.

create scheduled task powershell

In PowerShell 3.0 (it was introduced in Windows 8 and Windows Server 2012), you can create scheduled tasks in a much easier way.

Now you do not need to import the PowerShell module to work with scheduled tasks.

READ ALSO:  Fix The Drivers For This Device are Not Installed (code 28) error

Just create a new trigger object:

$Trigger = New-JobTrigger -Weekly -DaysOfWeek 2,4 -At 10:00PM

If you need run task from specific account, get it credentials:

$credent = Get-Credential theitbros\admin1

To create new task that should run with highest privileges, run the following command:

$elevat = New-ScheduledJobOption -RunElevated
Register-ScheduledJob -Name BackupDBTask2 -FilePath C:\ps\backupdb.ps1 -Trigger $Trigger  -Credential $credent –ScheduledJobOption $elevat

powershell run scheduled task

Tip. If you want to run task from Local System account, use the command:

Register-ScheduledJob -Name BackupDBTask2 -FilePath C:\ps\backupdb.ps1 -Trigger $Trigger  -User "System" –ScheduledJobOption $elevat

create scheduled task

To get a full list of the enabled scheduled tasks in the Microsoft\Windows\Powershell section, run the command:

Get-ScheduledTask -TaskPath \Microsoft\Windows\Pow*| ? state -ne Disabled

To get detailed information for a particular task, run:

Get-ScheduledTask BackupDBTask| Get-ScheduledTaskInfo

To disable particular task, use the Disable-ScheduledTask cmdlet:

Get-ScheduledTask BackupDBTask| Disable-ScheduledTask

scheduled task powershell

Leave a Reply

Your email address will not be published. Required fields are marked *