I’ve received this nice comment on one of my earlier posts and I thought it might warrant a blog post since it is a nice little challenge and might be useful for more scripters in our community.

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.

After this the content tree structure should look like on the following picture

In the simplest scenario I want to browse the created folder and and under each item of the “Sample Item” template I want to deploy my branch. For the sake of not having to create a branch template (which doesn’t really add anything to this exercise) I’ve picked one of the branches that already exists in vanilla Sitecore 7.1+ by default: /sitecore/templates/Branches/System/Rules/Taxonomy/Element Folder.

#Pull all children from our test folder
Get-ChildItem master:\content\branch-test -Recurse |
#filter only the items that are of specific template
Where-Object { $_.TemplateName -eq "Sample Item" } |
% {
#Create a branch based on some system branch in Polish language.
New-Item $_.ProviderPath -Name "test" -ItemType "Branches/System/Rules/Taxonomy/Element Folder" -Language pl-PL
#Add Danish, English and Japanese language to the newly created items based on the original Polish version.
Add-ItemLanguage -Path "$($_.ProviderPath)\test" -Language pl-PL -TargetLanguage da-DK, en, ja-JP -Recurse
}

Now I understand that you might not want to do it in all the languages on all of the items so to aid in this we will wrap the branch creation logic in a function: