Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

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 the Windows task scheduler?

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:

1. Start >> Administrative Tools >> Task Scheduler.  From the Actions menu, click on “Create a Basic Task”

windows task scheduler run powershell script

2. Give it a Name & Description Say: Content Databases Report, and click “Next”
windows task scheduler powershell script 
3. Select the interval you want to run the program. In my case, I chose “Monthly”
powershell script task scheduler windows 2008 r2

4. Specify the Months, Days in which the script to run. I’ve selected “All Months” and Day 1

schedule powershell script using task scheduler

5. In Action Tab, select the “Start a program” option button. Click Next
task scheduler to run powershell script  
6. In Start a Program Tab:

  • 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
powershell script scheduled task parameters
You must specify a value for the Start-in field, even though it’s optional. This is why because, if no value specified there, PowerShell will create the output in “C:\Windows\System32” directory.

7. Select the check box, “Open the Properties dialog for this task when I click Finish” and click the 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” checkboxes are selected.

You will get a login prompt on clicking the “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 the required modules such as PnP PowerShell or SharePoint Online PowerShell modules installed prior to scheduling them in the 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.
run scheduled task on demand

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  “”

So in Task scheduler, Add arguments(optional), Enter: d:\scripts\StorageReport.ps1  “”

When there are multiple parameters, you can separate them by giving parameter name as key. such as:

       [parameter(Mandatory=$true)] $WebAppURL,
       [parameter(Mandatory=$true)] $OutPut

# We trigger it as: StorageReport.ps1 -WebAppURL “” -output “d:\Reports\StorageReport.csv”

Here is how you can Run PowerShell Script from Batch files:
“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 a new cmdlet Register-ScheduledTask to create and manage Scheduled Tasks! Here is an example:

$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  

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply