Sample scripts for Sitecore PowerShell Console
November 18th, 2011 by Adam Najmanowicz | 429 CommentsFind out about your configuration:
List your available site providers. You will use the name provided in the "Name" column in place of where I use "cms" in the later samples.
get-psdrive ` | where-object { $_.Provider.ToString() -eq "CmsItemProvider"} ` | format-table Name, Root
Sample scripts for working with pages:
For the same of brevity I?m assuming your location is already somewhere within a Sitecore tree (e.g. you did something akin to cd master:\content prior to executing the following scripts.
List all properties & fields available for a particular page (including Sitecore properties defined in the item template).
get-childitem | get-member -memberType property*
List all items in the CMS of which template name contains "Article"
get-childitem -recurse ` | where-object { $_.TemplateName -match "Article" } ` | format-table DisplayName, Name, TemplateName
List all subitems and how many days ago were they modified
get-childitem -recurse ` | format-table -auto Name, ` @{Label="Days since modified"; Expression={ ` [datetime]::Now.Subtract($_.__Updated).Days} }
Delete all pages that have not been modified for the last 365 days.
get-childitem -recurse ` | where-object {[datetime]::Now.Subtract($_.__Updated).Days -gt 365 } ` | remove-item
List all items Updated over the last 24 hours and who changed them.
get-childitem -recurse ` | where-object { $_.__Updated -gt [datetime]::Now.AddDays(-1) } ` | format-table -property DisplayName, "__Updated By", {$_.Paths.Path}
List all pages that have their "Text" field filled in.
get-childitem -recurse ` | where-object { $_.Text -ne $null } ` | format-table -property DisplayName, {$_.Paths.Path}
Make a nice reviewer’s comment on all pages with their "Text" property filled in.
get-childitem -recurse ` | where-object { $_.Text -ne $null } ` | foreach-object { $_.ReviewersComment = "Great job providing content!" }
Add a warning to the beginning of "Text" property for all pages that have not been saved for the last 180 days.
get-childitem -recurse ` | where-object {` [datetime]::Now.Subtract($_.__Updated).Days -ge 180 ` -and $_.Text -ne $null } | foreach-object {$_.Text = "<p style='color:red'>Old content. Review pending!</p>" + $_.Text }
Replace a string in a property on all pages with another string (in this sample – removing the warning the last sample added).
$old_content = "<p style='color:red'>Old content. Review pending!</p>" $new_content = ""; get-childitem -recurse ` | where-object {$_.Text -match $old_content} ` | foreach-object {$_.Text = $_.Text.Replace($old_content, $new_content) }
Display the 10 most recently changed pages ordered in the reverse chronological order or changes. Display the page name, who changed it and when as well as the page status.
get-childitem -recurse ` | sort-object -property __Updated -descending ` | select-object -First 10 ` | format-table -property DisplayName, "__Updated By", __Updated
Sample scripts for working with Media Library / files:
Removes all .xml files from the ?Old Xml Files? in Media library.
cd "master:\media library\Old Xml Files" get-childitem -recurse ` | where-object { $_.Extension -match "xml" } ` | remove-item
Copy all files from the "staging" folder to the "production" folder in Media Library.
Copying files within media library is done as follows. This can also be done for items in the content branch.
copy-item "master:\media library\staging\*" "master:\media library\production\"