Setting Timer Jobs at on-premises SharePoint 2010, 2013, 2016, or 2019 using PowerShell

Note: The Timer Jobs only work with on-premises SharePoint and not SharePoint Online, as is a cloud-based version, the architecture and management of timer jobs are handled by Microsoft, and the end user do not have direct access to create or install custom timer jobs.


As a SharePoint Admin, there are a lot of cleaning, indexing, trigger workflow, and maintenance tasks that have to be performed periodically, in such a case you can make use of a timer job that can help run background processes on one or more SharePoint servers farms at a scheduled task periodically.

You can setup a timer job in SharePoint using PowerShell, by following the below steps:

    Step 1:

    Develop a custom timer job assembly and package it as a DLL file. The code within the assembly defines the behavior and tasks of your timer job.


    Step 2:

    Next, open the SharePoint Management Shell with administrative privileges.


    Step 3:

    Execute the following command to add the SharePoint snap-in, which provides the necessary cmdlets for managing SharePoint objects through PowerShell:

    Add-PSSnapin Microsoft.SharePoint.PowerShell

    Step 4:

    Create your timer job using the below code.

    $jobName = "CustomTimerJob"
    $timerJob = New-Object Microsoft.SharePoint.Administration.SPJobDefinition($jobName, (Get-SPServiceApplication | where {$_.TypeName -eq "Your_Service_Application_Type"}))

    Step 5:

    Set the scheduler

    $timerJob.Schedule = New-Object Microsoft.SharePoint.SPSchedule.Hourly
    

    Step 6:

    Now execute the below command to add the timer job to the SharePoint farm:

    $timerJob.Update()

    Step 7:

    Finally run the scheduler,

    $timerJob.RunNow()

    Comments & Discussion

    Facing issues? Have questions? Post them here! We're happy to help!