Today I was surprised to see duplicated records in SharePoint list which I imported and updated from XML-files. So I started to find a solution to find duplicated records, see them and delete.

Below I post a powershell script to find and remove duplicated records. It was found using google-search, and added string not to remove duplicated records immediately, but to see them at first.

 

 Add-PSSnapin microsoft.sharepoint.powershell 
    $web = Get-SPWeb -Identity http://spdev/vc 
    $list = $web.Lists["companies"] 

    $AllDuplicates = $list.Items.GetDataTable() | Group-Object title | where {$_.count -gt 1} 
    $count = 1 
    $max = $AllDuplicates.Count 
    foreach($duplicate in $AllDuplicates) 
    { 
        #uncomment string below to delete duplicated records
        #$duplicate.group | Select-Object -Skip 1 | % {$list.GetItemById($_.ID).Delete()} 

        #string below displays found duplicated records
        Write-Host $duplicate.Name 
    } 
Remove-PsSnapin Microsoft.SharePoint.PowerShell