We would like to pass parameters to powershell script calling from Task scheduler option powershell scripted task schedule.

enter image description here

Is there any functionality to pass custom parameters in powershell scripted task schedule under Task scheduler in Sitecore 9.1.1?

  • Please check the below link if that can help you: himmatsinghdulawat.blogspot.com/2021/07/… and himmatsinghdulawat.blogspot.com/2021/04/… Oct 8, 2021 at 8:32
  • I have checked this link and it does not have any thing related to parameters.
    – Ashish
    Oct 8, 2021 at 8:47
  • If you can change the scheduler approach with Class instead of Powershell then you can pass the parameter. Not sure you can change the approach or not due to your business requirement but it's my suggestion. How to pass parameter you can check this link himmatsinghdulawat.blogspot.com/2021/04/… Oct 8, 2021 at 10:05
  • Can you provide an example of what kind of parameters you want to pass. What will exactly the functionality of the task based on the parameters Oct 12, 2021 at 16:26
  • Need to pass value, for example : value=2 and it will be dynamically as we will use same script multiple time so we will pass dynamic parameters to powershall script from Task scheduler.
    – Ashish
    Oct 12, 2021 at 16:37

1 Answer 1


Using the OOTB powershell scripted task schedule, you will not be able to pass the parameters from the Tasks item itself. However, there are 2 possible solutions.

1st Solution

You can create a new folder that will host the parameters you want your script to have. From your PowerShell script, you will read that particular folder and then use the value stored as the parameter.

For example, you create an item folder named Tasks Parameters and create an item under Tasks Parameters named My Task Parameter. The item My Task Parameter will contain a single-line text field which holds the value to pass to your PowerShell script. Note, you will need to create a template first before being able to create those.

So, the pseudo code for your script will be similar to the following:

From your script, 
Navigate to item at path `/sitecore/system/Modules/Tasks Parameters`.
Reads the child item `My Task Parameter`.
Retrieve the value from the field
Use the value from the field as your parameter

With this solution, you don't need to have any custom code development but it may be difficult to manage.

2nd Solution

You will need to implement a command that will replace the PowerShell command that will trigger the task. Moreover, you will require to create your own template to hold the parameter. Then in your implementation, you can add the parameters to your script. Below are the steps:

Step 1

Create a new template which holds your custom command and parameters. You just need to create the field Custom Parameters and then inherits only the Command template (Template ID: {58119A3E-560E-4DA6-97C6-1ACE8A5B1219})

The field Custom Parameters is of type Name Value List. The main reason I am using the Name Value List field type is because it allows you to easily configure more than 1 variable.

enter image description here

Step 2

Create an item based on the new template above under the path /sitecore/system/Tasks/Commands. You may create a folder then create your command for a better content tree structure and management.

enter image description here

Step 3

Implement your custom command so that it can reads the field Custom Parameters and pass it to the PowerShell Script. Below is my current implementation.

using System.Collections.Generic;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Tasks;
using Spe;
using Spe.Core.Extensions;
using Spe.Core.Host;
using Spe.Core.Modules;
using Spe.Core.Utility;

namespace Zerex.Framework.Commands
    public class SpeTaskExtension
        public void Update(Item[] items, CommandItem command, ScheduleItem schedule)
            using (ScriptSession session = ScriptSessionManager.NewSession("Default", true))
                foreach (Item obj in items)
                    ProcessTaskItem(obj, command, session);

        private static void ProcessTaskItem(Item item, CommandItem command, ScriptSession session)
            if (!RulesUtils.EvaluateRules(ModuleManager.GetItemModule(item)?.GetFeatureRoot("tasks")?[Templates.ScriptLibrary.Fields.EnableRule], item))

            Queue<Item> objQueue = new Queue<Item>();


            Item obj1;

            var parameters = (NameValueListField)command.InnerItem.Fields["Custom Parameters"];

            var nameValueList = Sitecore.Web.WebUtil.ParseUrlParameters(parameters.Value);
            var keys = nameValueList.AllKeys;

            while (objQueue.Count > 0 && (obj1 = objQueue.Dequeue()) != null)
                if (obj1.IsPowerShellScript())
                    if (!string.IsNullOrWhiteSpace(obj1[Templates.Script.Fields.ScriptBody]) && RulesUtils.EvaluateRules(obj1[Templates.Script.Fields.EnableRule], obj1))
                        // Adding the parameters to the PS Script here
                        foreach (var key in keys)
                            session.SetVariable(key, nameValueList[key]);
                        session.ExecuteScriptPart(obj1, true);
                else if (obj1.IsPowerShellLibrary() && obj1.HasChildren && RulesUtils.EvaluateRules(obj1[Templates.ScriptLibrary.Fields.EnableRule], obj1))
                    foreach (Item obj2 in obj1.Children.ToArray())

Build and deploy the code.

Step 4

You need to update the namespace of your custom command with yours and also, you need to update the scheduled item command.

enter image description here


I have created a sample script that will update the Title field of the Sample Item. Your variable name will be the same as the one you used in the command configuration item. In the example I have provided above, the variable will be productId and value. In PowerShell, it is referenced as $productId and $value. Below is the script I have used to test this approach

$item = Get-Item -Path "master:/sitecore/content/Home/Sample Item"

New-UsingBlock (New-Object Sitecore.SecurityModel.SecurityDisabler) {
    $item.Fields["Title"].Value = "Prod: $productId and Value: $value"                                
    $item.Editing.EndEdit()| Out-Null


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.