Ad-hoc Reports In Powershell

Another quick one while I’ve got my head in Powershell mode. Sometimes you just want to whack some data in a report. Powershell is a great way to do this.

Here’s a script which gets all of the media items (for certain templates) and outputs the ID, TemplateID and Name:

$query = "fast:/sitecore/media library//*[@@templateid='{835508EA-798E-46C4-8C2B-FF7AF2A81BA5}' or @@templateid='{E3426006-6C9B-47A1-ABD9-6F40C00DC83B}' or @@templateid='{85850E09-0EE9-4102-8BAA-242C7536F6C4}']"
$out = new-object System.Collections.Generic.List[Object]
$items = Get-Item master: -Query $query -Language * | ForEach-Object {
	$outObj = new-object Object
		add-member -inputObject $outObj -membertype NoteProperty -Name Id -Value $_.ID
		add-member -inputObject $outObj -membertype NoteProperty -Name TemplateId -Value $_.TemplateID
		add-member -inputObject $outObj -membertype NoteProperty -Name Name -Value $_.Name


$out | Show-ListView -Property Id, TemplateID, Name

The biggest gotcha when processing items in this way is not to use @out = @( ) – this uses an underlying array so when you add to it (using +=) what this actually does is clone the array in to a new larger array. Once you get over a certain size this gets very slow.

Instead, we use a CLR List and the Add method.

Worth noting that you’d be much more likely to be using the index to pull these items out in reality – I just had this script to hand 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s