I’m trying to prevent the PowerShell Code from uploading Duplicate items from Excel and if it is already in the SharePoint list and skip the item…
there are 4 columns that can possible be duplicated.
“Title”
“field_2”
“field_3”
“field_4”
if all 4 match with what is already in the SharePoint list, then either do not upload it, or if after the items are loaded check for duplicates and then delete the new item. I’m not sure when to check for the duplicates.
Move Data to SharePoint
#Read the Excel Data and change path accordingly
$data = Import-Excel -Path "C:\<File Name>"
#Declare Site URL and List names
$SiteUrl = "https://<Site Url>"
# Update with the internal name of the SharePoint list.
$ListName = "<list Name>"
#Initiate a connection to SharePoint site
Connect-PnPOnline -Url $SiteUrl -UseWebLogin #-Interactive
try {
ForEach($record in $data) {
Write -Host "Adding record $($record.'<Sheet Name>')"
#Add List Items - Map with Internal Names of the Fields!.Below you define a custom object @ {}for each record and update the SharePoint list.
Add-PnPListItem -List $ListName -Values @{
"Title" = $($record.'<Excel Column>');
"field_2" = $($record.'<Excel Column>');
"field_3" = $($record.'<Excel Column>');
"field_4" = $($record.'<Excel Column>');
'field_5' = $($record.'<Excel Column>');
"field_6" = $($record.'<Excel Column>')
};
}
} catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Start-Sleep -Seconds 5
#Get Duplicate Rows based on Column: Title
$DataCollection | Sort-Object -Property Title | Group-Object -Property Title | Where-Object {$_.Count -gt 1} | Select-Object -ExpandProperty Group
Write-Host "Script End"
Start-Sleep -Seconds 5
exit