Should I say anything? The code explains itself 😊
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$sourceItems = Get-ChildItem -Recurse -Path "/sitecore/content/" | |
$query = "/sitecore/content//*[@@id!='{0}' and @OldUrl='{1}']" | |
$queryWithContains = "/sitecore/content//*[@@id!='{0}' and (@OldUrl='{1}' or contains(@OldUrl, '{2}'))]" | |
$foundDuplicatesCollection = New-Object System.Collections.ArrayList | |
Write-Host "SourceItemParentPath, SourceItemPath, SourceItemId, FoundDuplicateParentPath, FoundDuplicatePath, FoundDuplicateId, OriginalUrl, FoundDuplicateUrl" -ForegroundColor Green | |
foreach ($sourceItem in $sourceItems) { | |
$originalUrl = $sourceItem["OldUrl"] | |
if ($originalUrl -eq "") { | |
#Write-Host "Without original url - " $sourceItem.Paths.FullPath -ForegroundColor Magenta | |
continue | |
} | |
$builtQuery = "" | |
# change this condition if you need a different or remove it completely | |
# this condition checks links like old-domain.org?id=123456789 | |
if ($originalUrl -like '*?id=*') { | |
$uri = New-Object -TypeName System.Uri -ArgumentList $originalUrl | |
$id = [System.Web.HttpUtility]::ParseQueryString($uri.Query)["id"] | |
$builtQuery = [string]::Format($queryWithContains, $sourceItem.ID, $originalUrl, "?id=" + $id) | |
} | |
else { | |
$builtQuery = [string]::Format($query, $sourceItem.ID, $originalUrl) | |
} | |
#Write-Host $builtQuery | |
$foundDuplicates = Get-Item -Path "master:" -Query $builtQuery | |
foreach ($foundDuplicate in $foundDuplicates) { | |
$reverseResults = $foundDuplicatesCollection | Where-Object { $_.SourceItemId -eq $foundDuplicate.ID -and $_.FoundDuplicateId -eq $sourceItem.ID } | |
if ($reverseResults.Count -gt 0) { | |
continue | |
} | |
$result = New-Object System.Object | |
$result | Add-Member -MemberType NoteProperty -Name "SourceItemParentPath" -Value $sourceItem.Parent.Paths.FullPath | |
$result | Add-Member -MemberType NoteProperty -Name "SourceItemPath" -Value $sourceItem.Paths.FullPath | |
$result | Add-Member -MemberType NoteProperty -Name "SourceItemId" -Value $sourceItem.ID | |
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicateParentPath" -Value $foundDuplicate.Parent.Paths.FullPath | |
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicatePath" -Value $foundDuplicate.Paths.FullPath | |
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicateId" -Value $foundDuplicate.ID | |
$result | Add-Member -MemberType NoteProperty -Name "OriginalUrl" -Value $originalUrl | |
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicateUrl" -Value $foundDuplicate["OldUrl"] | |
$foundDuplicatesCollection.Add($result) | Out-Null | |
Write-Host $result.SourceItemParentPath"," -ForegroundColor DarkYellow -NoNewline | |
Write-Host $result.SourceItemPath"," -ForegroundColor DarkYellow -NoNewline | |
Write-Host $result.SourceItemId"," -ForegroundColor DarkYellow -NoNewline | |
Write-Host $result.FoundDuplicateParentPath"," -ForegroundColor Cyan -NoNewline | |
Write-Host $result.FoundDuplicatePath"," -ForegroundColor Cyan -NoNewline | |
Write-Host $result.FoundDuplicateId"," -ForegroundColor Cyan -NoNewline | |
Write-Host $result.OriginalUrl"," -ForegroundColor DarkYellow -NoNewline | |
Write-Host $result.FoundDuplicateUrl -ForegroundColor Cyan | |
} | |
} |
One thought on “Find item duplicates with Sitecore PowerShell”