Your First Scripts

Hands-on examples to get you started with practical Sitecore automation.

Now that you understand the basics, let's put it all together with practical Sitecore automation tasks. These examples demonstrate real-world scenarios you'll encounter when working with SPE.

Example 1: Find Items Ready for Review

Goal: Find all items under /sitecore/content/home that are in a specific workflow state.

# Navigate to the home item
$homeItem = Get-Item -Path "master:\content\home"

# Get all descendants
$items = Get-ChildItem -Path $homeItem.ProviderPath -Recurse

# Filter items in "Draft" workflow state
$draftItems = $items | Where-Object {
    $_.Fields["__Workflow state"] -ne $null -and
    $_."__Workflow state" -match "{190B1C84-F1BE-47ED-AA41-F42193D9C8FC}"
}

# Display results in an interactive list
$draftItems | Show-ListView -Property Name, ItemPath, "__Updated", "__Updated by"

What just happened?

  1. Retrieved the home item using the Sitecore provider → Learn more

  2. Queried all child items recursively

  3. Filtered by workflow field value → Learn more

  4. Displayed results as a report → Learn more

Example 2: Bulk Update Item Fields

Goal: Update a field value on multiple items.

Key concepts demonstrated:

  • Filtering by template → Templates

  • Proper item editing workflow → Editing Items

  • Using PowerShell loops and variables

Example 3: Interactive User Input

Goal: Create a script that prompts the user for input and processes items based on their choices.

Interactive concepts:

Try It Yourself

Ready to experiment? Follow these steps:

Step 1: Open the ISE

In Sitecore, go to Desktop → Development Tools → PowerShell ISE

Step 2: Run Your First Command

Try these commands in the script editor:

Step 3: Execute the Script

Click the Execute button (or press Ctrl+E)

Step 4: Explore the Results

  • Examine the object properties returned

  • Try modifying the filters

  • Experiment with different paths

The ISE provides IntelliSense, syntax highlighting, and debugging capabilities. It's the best place to learn and develop SPE scripts. See Scripting for more details.

Practice Exercises

Test your knowledge with these exercises:

Exercise 1: Find Empty Items

Write a script that finds all items under /sitecore/content that have an empty Title field.

Solution

Exercise 2: Count Items by Template

Write a script that counts how many items exist for each template under /sitecore/content/home.

Solution

Exercise 3: Export Item Data

Export all items under a specific path to a CSV file with their Name, Path, Template, and Last Updated date.

Solution

Common Patterns

Here are some frequently used patterns you'll encounter:

Pattern: Safe Item Editing

Pattern: Check if Item Exists

Pattern: Process Items with Progress

Next Steps

Congratulations! You've completed your first SPE scripts. Now:

  1. Avoid common mistakes: Review Common Pitfalls

  2. Build on this knowledge: Explore Working with Items

  3. Create custom reports: Learn Authoring Reports

  4. Share your scripts: Build a Script Library

Last updated