Commands and Pipelines
Learn PowerShell command syntax, pipelines, and essential commands.
Learning PowerShell begins with running your first command. In this section we learn about the basic command syntax, pipelines, and some common commands you should know.
Command Syntax
Example: The following provides an example syntax for a fake command.
Get-Something [[-SomeParameter] <sometype[]>] [-AnotherParameter <anothertype>] [-SomeSwitch]Verb-Noun Pattern
PowerShell commands follow a Verb-Noun syntax. Notice that all properly named commands start with a verb such as Get, Set, or Remove and end with a noun such as Item, User, or Role.
Examples:
Get-Item- Retrieves an itemSet-Item- Modifies an itemRemove-Item- Deletes an itemNew-Item- Creates an itemMove-Item- Moves an item
The verbs are considered "approved" if they align with those that Microsoft recommends. See the following URL https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx for a list of approved verbs and a brief explanation on why they were chosen. They are intended to be pretty generic so they apply for multiple contexts like the filesystem, registry, and even Sitecore!
Parameters and Arguments
The parameters follow the command and usually require arguments. In our example above we have a parameter called SomeParameter followed by an argument of type SomeType. The final parameter SomeSwitch is called a switch.
Parameter types:
Required parameters - Must be provided
Optional parameters - May be omitted (shown in
[square brackets])Positional parameters - Can be provided without the parameter name (shown with
[[double brackets]])Switch parameters - Boolean flags that enable/disable behavior
Parameter Examples
Example: The following provides possible permutations for the fake command.
Splatting Parameters
Instead of passing parameters inline, you can "splat" them using a hashtable:
When to use splatting:
Commands with many parameters
Reusable parameter sets
Conditional parameters
Improved readability
Best Practices for Scripts
Allow scripts to be written with the full command and parameter names:
✅ Do use full command names (not aliases)
✅ Do use full parameter names (not abbreviations)
✅ Do specify parameter names explicitly
❌ Avoid relying on positional parameters in scripts
❌ Avoid abbreviating parameter names
❌ Avoid using command aliases (e.g. dir, cd) in scripts
Example:
Essential Commands
Some of the most useful commands to learn can be seen in the table below. These come with vanilla PowerShell.
Get-Item
Returns an object at the specified path
Get-Item -Path "master:\content\home"
Get-ChildItem
Returns children at the specified path. Supports recursion
Get-ChildItem -Path "master:\content" -Recurse
Get-Help
Returns the help documentation for the specified command or document
Get-Help Get-Item
Get-Command
Returns a list of commands
Get-Command *Item*
ForEach-Object
Enumerates over the objects passed through the pipeline
$items | ForEach-Object { $_.Name }
Where-Object
Enumerates over the objects passed through the pipeline and filters objects
$items | Where-Object { $_.Name -like "*Test*" }
Select-Object
Returns objects from the pipeline with the specified properties and filters objects
$items | Select-Object Name, ID
Sort-Object
Sorts the pipeline objects with the specified criteria; usually a property name
$items | Sort-Object Name
Get-Member
Returns the methods and properties for the specified object
$item | Get-Member
Measure-Object
Calculates statistics on objects
$items | Measure-Object
Group-Object
Groups objects by property value
$items | Group-Object TemplateName
Pipelines
PowerShell supports chaining of commands through a feature called "Pipelines" using the pipe "|". Similar to Sitecore in that you can short circuit the processing of objects using Where-Object.
The Pipeline Concept
The pipeline passes objects from one command to the next:
Command1 produces output
Command2 receives that output, processes it, and produces new output
Command3 receives Command2's output and produces final output
Current Object Variables
Simple Pipeline Examples
Example: The following queries a Sitecore item and removes it.
Example: The following gets children and displays specific properties.
Complex Pipeline Example
PowerShell also comes with a set of useful commands for filtering and sorting. Let's see those in action.
Example: The following queries a tree of Sitecore items and returns only those that meet the criteria. The item properties are reduced and then sorted.
Common Pipeline Patterns
Filtering
Transforming
Grouping and Aggregating
Sorting
ForEach Processing
Getting Help
Windows PowerShell is bundled with a ton of documentation that could not possibly be included with this book; we can however show you how to access it.
Help Commands
Example: The following examples demonstrate ways to get help…with PowerShell.
Finding Commands
Discovering Properties and Methods
Readable vs Abbreviated Commands
Example: The following demonstrates how commands can be written clearly with little confusion on the intent, then how aliases and abbreviations can get in the way. Always think about the developer that comes after you to maintain the code.
Why use the longhand?
Easier to read
Self-documenting
No ambiguity
Works even if aliases change
Better for team collaboration
Common Aliases
While you shouldn't use aliases in scripts, it's helpful to recognize them:
?
Where-Object
Filter objects
%
ForEach-Object
Process each object
select
Select-Object
Choose properties
sort
Sort-Object
Sort objects
group
Group-Object
Group by property
measure
Measure-Object
Calculate statistics
gci
Get-ChildItem
Get children
gi
Get-Item
Get item
cd
Set-Location
Change directory
dir
Get-ChildItem
List directory
ls
Get-ChildItem
List directory
View all aliases:
Next Steps
Now that you understand commands and pipelines:
Learn about providers: Providers
Practice with examples: Your First Scripts
Avoid common mistakes: Common Pitfalls
Deepen your knowledge: Working with Items
The pipeline is PowerShell's superpower! Master it and you'll be able to accomplish complex tasks with surprisingly little code.
Last updated