Create a Scheduled Task to Run PowerShell Script
Requirement: Schedule PowerShell Script in Windows Task Scheduler.
PowerShell is really a game-changer to automate repetitive or time-consuming processes, isn’t it? We have a PowerShell script to generate a report on SharePoint content databases size growth – Storage Report, We used to run it on the first day of every month on the SharePoint server to generate the report. Why don’t we automate it with the Windows Task Scheduler? Sure! Let’s create a scheduled task for the PowerShell script with the Microsoft Windows task scheduler in Windows Server 2008/2012 R2. In this blog post, I’ll show you how to create a Scheduled task to run a PowerShell script using the Windows Task Scheduler step-by-step.
How to Create a Scheduled Task for PowerShell Script with Windows Task Scheduler?
You can automate your PowerShell scripts with the Windows task scheduler. Here is how you can create Scheduled Tasks manually:
- Open the Task Scheduler by going to Start Menu >> Administrative Tools >> Task Scheduler.
- From the Actions menu, click on “Create a Basic Task”.
- Give it a Name and Description. Say: “Content Databases Report”, and click “Next”.
- Select the interval you want to run the program, such as “Daily”, “Weekly”, etc. In my case, I chose “Monthly”.
- Specify the Months, Days in which the script is to run. I’ve selected “All Months” and Day 1.
- In Action Tab, choose what action you want the task to perform. For our purposes, we’ll select the “Start a program” option button. Click Next.
- In the “Start a Program” Tab:
- In Program/script, Enter the path of the Windows PowerShell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- In Arguments, Enter the path of the script. Say: D:\Scripts\ContentDatabaseReport.ps1
- In “Start in”, Enter the folder path where the script is located. Say “D:\Scripts”
Important: You must specify a value for the Start-in field, even though it’s optional. This is because if no value is specified, PowerShell exports the output in the “C:\Windows\System32” directory.
- Select the checkbox “Open the Properties dialog for this task when I click Finish”, and click the Finish button.
- In the properties dialog, under the General tab, ensure that the “Run when user is logged on or not” and “Run with highest privileges” checkboxes are selected to ensure you are running the script with Administrator rights.
- You will get a login prompt by clicking the “OK” button. Confirm the User Name & Password in which the task runs (preferably, a service account with a password never expires flag set) and press enter. The task scheduler will create a new task to run the PowerShell script on given parameters.
You can use this method to schedule SharePoint Online PowerShell scripts as well. Just make sure you have the required modules, such as PnP PowerShell or SharePoint Online PowerShell modules, installed before scheduling them in the Windows task scheduler.
Create Tasks in Task Scheduler Command Line:
We can add a PowerShell script to task scheduler with the command line tool schtasks too! Use the following command:
schtasks /create /TN “Content Database Report” /SC monthly /mo 1 /ST 00:00 /RU “Domain\UserName” /RP “Password” /RL “HIGHEST” /TR “PowerShell D:\Scripts\ContentDatabaseReport.ps1”
This will set the options “Run with highest privileges” and “Run whether the user is logged on or not” for the Scheduled Task.
Run the Scheduled Task on demand:
To run a PowerShell script from the task scheduler,
- Open task scheduler >> Browse to “Task scheduler Library” >> Right-click on the created task and choose Run.
- Switch over to the script file location and verify a new report has been generated as an output from the script.
That’s all! We’ve created a task in the Windows task scheduler for the PowerShell script!
PowerShell script with parameters in a scheduled task:
Say, We have a Parameter “$WebAppURL” in our PowerShell script: Param( [parameter(Mandatory=$true)] $WebAppURL), then we should trigger the script as: .\StorageReport.ps1 “https://sharepoint.crescent.com”
So in the Task scheduler, Add arguments (optional), Enter d:\scripts\StorageReport.ps1 “https://sharepoint.crescent.com”
When there are multiple parameters, you can separate them by giving the parameter name as the key. Such as:
Param(
[parameter(Mandatory=$true)] $WebAppURL,
[parameter(Mandatory=$true)] $OutPut
)
We trigger it as: StorageReport.ps1 -WebAppURL “https://sharepoint.crescent.com” -output “d:\Reports\StorageReport.csv”
“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” -command “<Your PowerShell Script Location>”
Create Scheduled Tasks with PowerShell:
From PowerShell 3.0 (Windows Server 2012 R2) onwards, We have PowerShell cmdlets to manage tasks in the Task scheduler. You can create/delete/enable/disable tasks using PowerShell.
Use Register-ScheduledTask to create a Scheduled Task! Here is an example:
#Variables
$TaskName = "Audit Large Lists"
$username ="Crescent\SP13_FarmAdmin"
$password ="Password Here"
#create a scheduled task with powershell
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "E:\Scripts\AuditLists.ps1"
$Trigger = New-ScheduledTaskTrigger -Daily -At 1am
$ScheduledTask = New-ScheduledTask -Action $action -Trigger $trigger
Register-ScheduledTask -TaskName $TaskName -InputObject $ScheduledTask -User $username -Password $passwordÂ
Enter the parameters for the task you want to create, including the task name, action (this is what you want the task to do), trigger (when you want the task to run), user name, and password. Once you have entered all this information, execute the script, and the task will be created! To create and Manage Scheduled Tasks using PowerShell, refer: How to Create a Scheduled Task using PowerShell?
Wrapping up
Creating a scheduled task to run a PowerShell script using Windows Task Scheduler is a great way to automate repetitive tasks. By following the steps above, you can quickly get started with setting up your own scheduled tasks. You can also schedule a PowerShell script with Azure Automation! SharePoint Online: How to Schedule a PowerShell Script using Azure Automation?
To schedule a SharePoint PowerShell script to run in Task Scheduler, ensure you have the necessary modules, such as “Microsoft.SharePoint.PowerShell” or PnP.PowerShell is installed on the machine first. Then proceed with the below steps to create a scheduled task for the SharePoint PowerShell script:
Open Task Scheduler >> Create a Basic Task >> Provide a Name to your Task >> Set the trigger to run the task daily or at a specific time. Set the action to “Start a program”. In the “Program/script” field, enter the path to PowerShell.exe: “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”. In the “Add arguments” field, enter the path to your PowerShell script: “-File C:\Path\To\Your\Script.ps1”. Click “OK” to save the task.
To run a PowerShell script with elevated privileges from Task Scheduler, in Task properties – select the “Run with highest privileges” option under the General tab!
Use the Register-ScheduledTask cmdlet to create a new scheduled task on the computer, passing the trigger and action as parameters. E.g.,
$Trigger = New-ScheduledTaskTrigger -Daily -At 10am
$Action = New-ScheduledTaskAction -Execute “Powershell.exe” -Argument “D:\Scripts\ReportGen.ps1”
Register-ScheduledTask -TaskName “Execute Report Gen Powershell” -Trigger $Trigger -Action $Action