1

Environment: Sitecore 9.2 & SXA 1.9

Scenario: I have two different items and I would like to compare them and copy the missing children from one item to another using Powershell script.

  • Item A (DifferenceObject) - If empty copy all the children from Item B and if few children are missing then copy them from Item B without disturbing the other children of Item A.

  • Item B (ReferenceObject) - This item can have grandchildren as well.

This is the script that I am using but while running this script on the same item twice, it is making duplicate copies of children instead of ignoring them.

$itemA = Get-ChildItem -Path "/sitecore/home/itemA"
$itemB = Get-ChildItem -Path "/sitecore/home/itemB"

$diffs = Compare-Object -ReferenceObject $itemB -DifferenceObject $itemA | 
Where-Object { $_.SideIndicator -eq "<=" } | Select-Object -ExpandProperty InputObject

$diffs | Copy-Item -Destination $itemA.ID -Recurse

How to achieve this functionality? Thanks.

3
  • Have you tried anything already? – Marek Musielak May 12 at 11:20
  • @MarekMusielak Yes, I used Compare-Object function, while I running the script twice on for the same item it is making duplicate copies instead of ignoring them. – PSS May 12 at 11:26
  • @MarekMusielak Please check the edit. Thanks. – PSS May 12 at 11:36
1

I was able to copy child items from one folder to another using this simple script:

$targetPath = "/sitecore/content/home/test2/"

$itemAChildren = Get-ChildItem -Path "/sitecore/content/home/test1" -recurse
$itemBChildren = Get-ChildItem -Path "/sitecore/content/home/test2" -recurse

foreach ($itemAChild in $itemAChildren) {
    $exists = 0
    $createpath = $targetPath

        foreach ($itemBChild in $itemBChildren) {
            if ($itemAChild.Name -eq $itemBChild.Name) {
                $exists = 1
            }
        }
        
        if ($itemAChild.Parent.Paths.Path -eq "/sitecore/content/Home/Test1") {
            $createpath = $targetPath
        }
        else
        {
            $createpath = $itemAChild.Paths.Path
            $createpath = $createpath -replace "/sitecore/content/Home/Test1", "/sitecore/content/home/test2"
        }  
            
        if($exists -eq 0) {
            Copy-Item -Path $itemAChild.ItemPath -Destination $createpath
        }
    }
4
  • Hello Cristi, this script would be fine when an item has children but in the case of grandchildren it will not perform as desired. I tried to -Recurse both items, it is copying the grandchildren under the item directly instead of the item's children. – PSS May 12 at 12:29
  • So you are trying to copy the whole structure of children from all levels? – Cristi Vulturar May 12 at 12:44
  • Yes, that's why I was trying to use Compare-Object. Do you have any suggestions on how to implement this functionality without writing multiple foreach for child/grandchild contains children? – PSS May 12 at 12:48
  • I updated the answer, what would remain would be the check of the existence as there can be multiple items with the same name. I think there it must be added a line to check also the name of the parent – Cristi Vulturar May 12 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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