I'm writing a powershell script to be applied to a SharePoint 2013 list. The script essentially keeps List B synced with List A.. The script works fine for adding list items from List A to List B when they don't exist in List B, but I'm having a problem with the part of the script that deletes an item in List B if it does not exist in List A.
Lists A and B have two columns, Title and Description columns and it is these columns that are utilised in the arrays.
Before posting the part of the script I'm having an issue with, the working parts of my script are ...
Add all items from List A into an array $arrA Add all items from List B into an array $arrB
Using nested foreach statements compare every item from $arrA with every item in $arrB and if there is no match (it's unique in $arrA and therefore List A), add it to $arrC.
Do the same for unique items in $arrB and therefore List B and add them to $arrD
This bit works as I have write-host statements to confirm the contents of $arrC and $arrD.
The next part of the script adds items from $arrC into List B - this works fine.
The next snippet of code is intended to take every object stored in $arrD, compare it with items in List B and if there's a match, delete the item in List B.
One further thing to point out is the Title column in List B was renamed "Service Component", but it's internal name is still Title. The array uses "Service Component" however. Here's the code ...
write-host "Items to delete from List B ... "
foreach($iD2 in $arrD)
{
write-host "Checking this ARRAY item ... " $iD2."Service Component" " " $iD2.Description " END"
foreach ($bListItem in $bList.Items)
{
write-host "Checking this LIST item ... " $bListItem.Title " " $bListItem.Description " END"
if ($bListItem.Title -eq $iD2."Service Component" -And
$bListItem.Description -eq $iD2.Description)
{
write-host $bListItem.Title " will be deleted"
$bListItem.delete()
break
}
}
}
Here's the output from my code.
Items to delete from List B ...
Checking this ARRAY item ... Test 13 <div class="ExternalClass33EFF8598CC041089D02F51385CFDE6E"><p>Desc</p></div> END
Checking this LIST item ... Test1 END
Checking this LIST item ... Test2 END
Checking this LIST item ... Test3 END
Checking this LIST item ... Test 13 END
As can be seen the array item "Test 13" does not match the List Item "Test 13"
I can see the array item contains all object properties, but the List item stored in variable $bListItem only stores the Title and ignores the description, and therefore the array item and list item don't match. In one of my tests, the list item was deleted when the description field was blank. I don't understand why my list item variable is only storing the Title and not the description field. I need to use both fields to find an exact match and delete the appropriate list item.
I was trying to follow advice from the following link which seems to have set up the iteration through a list with the following code which seems the same as mine:
foreach ($listItem in $list.Items)
Iterating through a SharePoint List and Items with PowerShell
Any advice would be much appreciated. I'm expecting to iterate through each array item, and for each array item iterate through the list items and when there's a match based on Title AND Description, delete the list item.