Create a Scheduled Task to Run PowerShell Script with Windows Task Scheduler

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 PowerShell script with 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:

  1. Open the Task Scheduler by going to Start Menu >> Administrative Tools >> Task Scheduler. 
  2. From the Actions menu, click on “Create a Basic Task”.windows task scheduler run powershell script
  3. Give it a Name and Description. Say: Content Databases Report, and click “Next”.windows task scheduler powershell script
  4. Select the interval you want to run the program, such as “Daily”, “Weekly”, etc. In my case, I chose “Monthly”. powershell script task scheduler windows 2008 r2
  5. Specify the Months, Days in which the script is to run. I’ve selected “All Months” and Day 1. schedule powershell script using task scheduler
  6. 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. task scheduler to run powershell script
  7. In 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”powershell script scheduled task parameters
      Important: You must specify a value for the Start-in field, even though it’s optional. This is why because, if no value is specified there, PowerShell exports the output in “C:\Windows\System32” directory.
  8. Select the checkbox, “Open the Properties dialog for this task when I click Finish” and click the Finish button.
  9. 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.
  10. You will get a login prompt on clicking the “OK” button. Confirm the User Name & Password in which the task runs (preferably, a service account with 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.

Don’t forget to set the Execution Policy! Set-ExecutionPolicy RemoteSigned, How to set the Execution Policy in PowerShell?

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 PowerShell script from 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.
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 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  “”

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

When there are multiple parameters, you can separate them by giving the parameter name as the 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 in command prompt:
“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.

PowerShell cmdlets for Task scheduler

Use Register-ScheduledTask to create a Scheduled Task! Here is an example:

$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 name of the task, action (this is what you want the task to do), trigger (when do 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!

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 easily 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?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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