Requirement: Schedule PowerShell Script in Windows Task Scheduler.
PowerShell is really a game-changer for repeatable processes, isn’t it? We have a PowerShell script to generate a report on SharePoint content databases size growth SharePoint Content Databases Size – 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 Windows Task scheduler? Sure! Let’s create a scheduled task for PowerShell script with Windows task scheduler in Windows Server 2008/2012 R2. How to schedule a PowerShell script using Windows task scheduler?
How to Create a Scheduled Task for PowerShell Script with Windows Task Scheduler?
You can automate your PowerShell scripts with windows task scheduler. Here is how you can create Scheduled Tasks manually:
1. Start >> Administrative Tools >> Task Scheduler. From Actions menu click on “Create a Basic Task”
4. Specify the Months, Days in which the script to run. I’ve selected “All Months” and Day 1
- In Program/script, Enter: 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 path where the script is located. say: D:\Scripts
7. Select the check box, “Open the Properties dialog for this task when I click Finish” and click Finish button.
8. Now, in the properties dialog, under the General tab, Make sure that the “Run when user is logged on or not” and “Run with highest privileges” check boxes are selected.
You will get a login prompt on clicking “OK” button. Enter the User Name & Password in which the task runs. You can use this method to schedule SharePoint Online PowerShell scripts as well. Just make sure you have required module such as PnP PowerShell or SharePoint Online PowerShell modules installed prior scheduling them in Windows task scheduler.
Create Tasks in Task Scheduler Command Line:
We can create a scheduled task with the command line tool schtasks too!
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.
To Run the Scheduled Task on-demand:
- Right click on the created task and choose Run.
- switch over to the script file location and verify new report has been generated as an output from the script.
That’s all! We’ve created a task in windows task scheduler for PowerShell script!
PowerShell script with parameters in scheduled task:
Say, We’ve a Parameter “$WebAppURL” in our PowerShell script:
Param( [parameter(Mandatory=$true)] $WebAppURL)
# We’ll trigger the script as: .\StorageReport.ps1 “http://sharepoint.crescent.com”
So in Task scheduler, Add arguments(optional), Enter: d:\scripts\StorageReport.ps1 “http://sharepoint.crescent.com”
When there are multiple parameters, you can separate them by giving parameter name as key. such as:
# We trigger it as: StorageReport.ps1 -WebAppURL “http://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) on wards, We’ve a new cmdlets to create and manage Scheduled Tasks! Here is an example:
#Variables $TaskName = "Audit Large Lists" $username ="Crescent\SP13_FarmAdmin" $password ="Password Here" #Create Scheduled Task $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