Find 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\"
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading...



This entry (Permalink) was posted on Friday, November 18th, 2011 at 1:24 pm and is filed under .Net Framework, ASP.NET, C#, Code Samples, PowerShell, Sitecore, Software, Software Development, Solution, Web applications. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.



  • Pingback: State of PowerShell for Sitecore for April 2012 | Codality()

  • Prabhat

    Hi , I want to delete all versions of english language of an item , any idea how can i achieve this ?

  • Hi Prabhat,

    How I would go about it would be to pipe all the versions into a code block and then just call Sitecore API for that, like:

    get-item ItemPath -Language en -version * | %{ $_.Versions.RemoveVersion() }

    What it does is it filters all items by their language “en” forcing only the english versions to pass, but than also the -Version parameter forces PowerShell to produce all versions of that language at the exit of the pipe. now once that is done, the standard Sitecore API call is performed on each of those item versions.

    What’s cool about it is that construction is you can also do this on whole tree of items by using “Get-Childitem -recurse” instead of Get-Item :)

    Happy Scripting!

  • Ravi Gupta

    Hi I want to create a content tree of my website. Is this possible to create items in sitecore as per specified sitemap using powershell ?

  • Hi Adam. Not sure if you’re monitoring this – the get-help on commands inside the console seem to display object properties rather than the normal powershell help. Same issue with get-command. in regular powershell I would get something similar to a format-table result from the standard get-command method. In Sitecore PowerShell console I need to use “get-command | format-table commandtype, name, modulename” to get the same result (and the formatting isn’t anywhere near as nice for some reason).
    Any thoughts? Is this some kind of display default?

    EDIT: If I use format-table in a regular powershell the formatting is the same as SC PS, but using the plain get-command in the two consoles give dramatically different results.

  • Hi Richard – I wonder what version of PowerShell are you running on the server and do you run both of the commands on the same runtime i.e. on the server the Sitecore is running on. There can be some differences based on whether the underlying PowerShell is 2.0 or 3.0.

    Could you perform Get-Host on both and paste it here?

    It should look somewhat like:

    Name : Sitecore Console Host 2.6.1.4986 on Windows PowerShell 4.0
    Version : 2.6.1.4986
    InstanceId : dba4169b-886d-4372-b616-7809625377f2
    UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture : pl-PL
    CurrentUICulture : en-US
    PrivateData : Cognifide.PowerShell.PowerShellIntegrations.Host.ScriptingHostPrivateData
    IsRunspacePushed : False
    Runspace : System.Management.Automation.Runspaces.LocalRunspace

    Especially the first 2 lines are meaningful.

    If your PowerShell is less than version 3 could you install the latest bersion of the Windows Management framework and see if that helps?

  • Sorry for the late reply – I didn’t notice the comment before…

    Very much so – you can look at the script located at:
    Toolbox -> Re-create site from sitemap

    in the SPE script library to see how create a site from sitemap.

  • PS master:contentxxxx homeemail campaignrecipient lists>get-host

    Name : Sitecore Console Host 2.5.5.38542 on Windows PowerShell 2.0

    Version : 2.5.5.38542

    InstanceId : f276349d-e23c-4154-b103-db8118cd33bc

    UI : System.Management.Automation.Internal.Host.InternalHostUserInterface

    CurrentCulture : en-AU

    CurrentUICulture : en-US

    PrivateData : Cognifide.PowerShell.PowerShellIntegrations.Host.ScriptingHostPrivateData

    IsRunspacePushed : False

    Runspace : System.Management.Automation.Runspaces.LocalRunspace
    If i run get-host on the server (as in PowerShell proper) I get v2.0. It is Windows Server 2008 R2.
    I will update PowerShell on the server now and see if that helps.

  • For the record – we’ve solved the issue over mail. The final conclusion was that the problem is with the Execution Policy with the PowerShell on the server instance.

    If you’ll execute the Get-ExecutionPolicy commandlet I suspect the value you will see is “AllSigned”.

    What you need to do to solve the issue is to Run a Windows PowerShell on
    the server with Administrative privileges and use the following command
    to allow SPE to attach itself and load a proper profile:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

    Now any new PowerShell session in your Sitecore instance will be showing results as expected for Get-Help and Get-Command.

  • Nona Durham

    Great Info! Much needed.

  • Thanks Nona!

    Being exposed to PowerShell for so long I tend to forget this might still be an issue for anyone with freshly installed PowerShell. Perhaps it is worth having an indicator in ISE and the console if the execution policy is set to AllSigned?

  • I’ve created a short script for this for you [on Gist] to do just that.
    You will probably need to customize it somewhat but it’s a start.

  • You’re most welcome :)

  • I would begin with the following:

    $path = "master:content"
    @(Get-Item -Path $path -Language "en*") + @(Get-ChildItem -Path $path -Language "en*" -Recurse)

    Once you know for sure that this is the list of items you want to remove, you can pipe those results to Remove-ItemLanguage.

    $path = "master:content"
    @(Get-Item -Path $path -Language "en*") + @(Get-ChildItem -Path $path -Language "en*" -Recurse) | Remove-ItemLanguage -Language "en*"

    Check out our Gitbook for more details here. The section about working with items covers getting by version and language.

  • Andrew

    Hey this is a great resource, thank you very much!

    I’m quite new to Powershell and Sitecore. Is it possible to list all the modules in the layout of a template using Powershell?

    E.g via the Sitecore GUI I would go to the standard values of a template, then in the ribbon go Presentation > Layout > Data to see this. Is that possible to do this via a script?

    I plan to spend some time with the book etc. over the next few days :) Thanks again!

  • Jon Upchurch

    I ended up trashing a VM instance because this was happening to me and I couldn’t figure out why! Good to know for the future.

  • Phil Neale

    Hi Adam, thanks for the pointers on PowerShell! There’s one in particular I’m hoping to get some help with, if that’s ok? I have created a Branch Template in Sitecore, and I want to deploy it under every item in the Content Tree that uses one particular page template (about 200 instances of deployment, in this case). For an added bit of fun, there will be several different language versions being deployed along the way, so I guess that prevents me doing a blanket rollout. Could you please recommend a script that is along the lines of “Add this Branch Template under this Item ID in the following named language codes”? I realise I would then be repeating that line 200 times with varying parent item ID and languages, but I can live with that if that’s the easiest way to do it.

    Many thanks for your help!

  • Pierre Sapinault

    Hi Adam,
    I have a bit of a challenge here, not even sure it’s possible. We are updating production servers with a new version of our websites on a different device. We do not want to reset the Default device. We have added the new layout and renderings on the template’s standard values for the new device. My question is: is there a way to reset the layout for a specific device only?
    My guess is no but maybe you know a way!
    PS: we are using sitecore 7.2
    best,
    Pierre

  • Anil N

    Hi,
    i require script which will iterate through all child items of sitecore content and check whether one of field(ex: Active (checkbox) of each items is checked or unchecked.
    ex: Get-childitems master:contenthomeProducts

    pls help on this

    Regards,
    Anil


  • #To list all products that are active
    Get-Childitem master:content -Recurse |
    Where-Object { $_.Active -eq "1" } |
    Format-Table # or do something with them

    #To list all products that are NOT active
    Get-Childitem master:content -Recurse |
    Where-Object { $_.Active -ne "1" } |
    Format-Table # or do something with them

    #To see all products that are NOT active
    Get-Childitem master:content -Recurse |
    Show-Listview -Property Name, ProviderPath, @{Label="Active"; Expression={$_.Active -eq "1" }}

  • Anil N

    Thanks for reply.

    i have one more query. to list all products from product repository which is contains item bucket .(bucket template)

    here is my structure:
    +Product Repository
    -2015
    -06
    -04
    -14
    -07

    pls help on this

  • The -Recurse parameter should deal with items regarding of whether they are in buckets or not. you can add an additonal filter somewhere in the middle that would test the template though if you want to disregard the bucket folders e.g. something like the following as the second line of your pipeline:

    Where-Object { $_.TemplateName -eq "Product" } |

  • Dev

    Hi Adam,

    I am new to powershell and have a situation were I need to change a bunch of items from unversioned to versioned code in a specific template. Can you please help on this.