I'm writing a SPE script to package up all unused media library items under a specific folder (prior to deletion). I've found New-Package and New-ItemSource methods and know how to use them (to some extent). How can I add multiple items from a collection to a single ItemSource object?

My script so far looks like this:

$items = Get-ChildItem -Path "master:\sitecore\media library\Project\ACME" -Recurse | 
Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder} |
Where-Object { ![Sitecore.Globals]::LinkDatabase.GetReferrerCount($_) -gt 0}


# Create package
$package = new-package "Unused Media Items";

# Set package metadata
$package.Sources.Clear();
$package.Metadata.Author = "ACME";
$package.Metadata.Publisher = "ACME";
$package.Metadata.Version = "1.0.0";
$package.Metadata.Readme = "";

$source = $items | New-ItemSource -Name 'Media' -InstallMode Overwrite
$package.Sources.Add($source);

It seems that when I pipe a collection object to New-ItemSource it only picks up the last item. It seems wrong to create a new itemsource object for each item, but maybe that is not an issue?

up vote 7 down vote accepted

The script we use to bundle up SPE can be found here: /sitecore/system/Modules/PowerShell/Script Library/Platform/Development/PowerShell Extensions Maintenance/Prepare Console Distribution.

From the documentation here:

Creates new Item source that can be added to a Sitecore package. Item provided to it is added as well as its subitems.

New-ItemSource returns a single object which represents the root node. Should probably just use Get-Item.

Get-Item 'master:\templates\Modules\PowerShell Console' | 
    New-ItemSource -Name 'Master Item Templates' -InstallMode Overwrite

enter image description here


Creates new Item source that can be added to a Sitecore package. This source only includes items explicitly added to it and not their children.

New-ExplicitItemSource returns a single object which represents the multiple entries. Use Get-ChildItem.

Get-ChildItem 'master:\system\Modules\PowerShell' | 
    New-ExplicitItemSource -Name "Master Module Root" -InstallMode Merge -MergeMode Merge

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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