删除SharePoint列表中的重复项目(Delete Duplicate Items In SharePoint List)
我有一个包含数千条记录的列表,我需要它们是唯一的。 我在网上找到了一个PowerShell脚本,如果我只有一个唯一的列,它将起作用。 但是,我必须按2列分组,我无法弄清楚如何使其工作。
例如,如果我在SP列表中包含此数据,则仅当两列都重复时才应删除这些项。
Title Carrier 1 Carrier1 1 Carrier1 *Remove This One 12 Carrier1 12 Carrier2 100 Carrier1 100 Carrier1 *Remove This One 100 Carrier2
以下是我在网上找到的代码示例,它适用于1列,但不适用于2列。
cls if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell; } $ListName = "DuplicateTest" $web = Get-SPWeb -identity "http://MyVM/sites/TestSite" $list = $web.Lists[$ListName] $AllDuplicates = $list.Items.GetDataTable() | Group-Object Title | where {$_.count -gt 1} $count = 1 $max = $AllDuplicates.Count foreach($duplicate in $AllDuplicates) { $duplicate.group | Select-Object -Skip 1 | % {$list.Items.DeleteItemById($_.ID)} Write-Progress -PercentComplete ($count / $max * 100) -Activity "$count duplicates removed" -Status "In Progress" $count++ }
I have a list which has thousands of records and I need them to be unique. I found a PowerShell script online that will work if I only have 1 unique column. However, I have to group by 2 columns and I can't figure out how to make it work.
For example, if I have this data in a SP list, only if both columns are duplicates should the items be removed.
Title Carrier 1 Carrier1 1 Carrier1 *Remove This One 12 Carrier1 12 Carrier2 100 Carrier1 100 Carrier1 *Remove This One 100 Carrier2
Here is the code sample I found online that will work for 1 column, but not for 2.
cls if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell; } $ListName = "DuplicateTest" $web = Get-SPWeb -identity "http://MyVM/sites/TestSite" $list = $web.Lists[$ListName] $AllDuplicates = $list.Items.GetDataTable() | Group-Object Title | where {$_.count -gt 1} $count = 1 $max = $AllDuplicates.Count foreach($duplicate in $AllDuplicates) { $duplicate.group | Select-Object -Skip 1 | % {$list.Items.DeleteItemById($_.ID)} Write-Progress -PercentComplete ($count / $max * 100) -Activity "$count duplicates removed" -Status "In Progress" $count++ }
原文:https://stackoverflow.com/questions/21337158