Working with Items
The majority of scripts written with SPE contain one or more of the following commands:
Get-Item
Get-ChildItem
New-Item
Remove-Item
Move-Item
Copy-Item
- Use this to retrieve a single item. Throws an error if the item does not exist.
- Use when Sitecore
query:
orfast:
is required. May return more than 1 item.
Get-Item -Path "master:\content\home"
- Use to return an item's children and grandchildren.
Get-ChildItem -Path "master:\content\home" -Recurse
- Use to create an item based on a specified data template.
New-Item -Path "master:\content\home" -Name "Demo" -ItemType "Sample/Sample Item"
# or
New-Item -Path "master:\content\home" -Name "Demo" -ItemType "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
- Use to delete or recycle an item.
- Accepts items returned by
Get-Item
andGet-ChildItem
.
Get-Item -Path "master:\content\home\delete-me" | Remove-Item
- Use to transfer an item from one location to another.
Move-Item -Path "master:\content\home\Demo" -Destination "master:\content\home\Demo1"
- Use to duplicate an item from one location to another.
# Add -PassThru to output the new item
Copy-Item -Path "master:\content\home\Demo1" -Destination "master:\content\home\Demo2"
The following commands provide you with the core methods needed to manage your content. Due to the nature of Windows PowerShell, commands such as these are extended with custom parameters and switches using Dynamic Parameters. These parameters are then added to the command at the time of use and only appear when the conditions are right. We've provided this table to help you discover the hidden gems within each command.
Parameter Name | Description | Copy-Item | Get-Item | Get-ChildItem | Move-Item | New-Item | Remove-Item |
AmbiguousPaths | More than one item matches the criteria so show them all. | – | ✓ | - | – | – | – |
Database | The specified database will be used. Requires the ID to be set. | – | ✓ | – | – | – | – |
DestinationItem | Parent item to receive the copied item. | ✓ | – | – | ✓ | – | – |
FailSilently | Unauthorized access errors will be suppressed | ✓ | – | – | ✓ | – | ✓ |
ForceId | Forces the new item to use a specified GUID | – | – | – | – | ✓ | – |
ID | Matches the item by ID. | – | ✓ | ✓ | – | – | – |
Item | Instance item. | ✓ | – | ✓ | ✓ | – | ✓ |
Language | Specifies the languages to include. | – | ✓ | ✓ | – | ✓ | – |
Parent | Specifies the parent item. | – | – | – | – | ✓ | – |
Permanently | Specifies the item should be deleted rather than recycled. | – | – | – | – | – | ✓ |
Query | Matches the items by an XPath query. | – | ✓ | – | – | – | – |
StartWorkflow | Initiates the default workflow, if any. | – | – | – | – | ✓ | – |
TransferOptions | Options flag used when copying from one database to another. | ✓ | – | – | ✓ | – | – |
Uri | Matches the item by ItemUri. | – | ✓ | – | – | – | – |
Version | Specifies the version to include. | – | ✓ | ✓ | – | – | – |
WithParent | Specifies the command should include the parent item. | – | - | ✓ | – |