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.

  • 1
    what content of description is retrieved if u access it by field operator: $bListItem.["description"] ? – Celophysis Aug 26 '16 at 14:20
  • Thanks for the response, it doesn't like the square brackets / quotation marks - when using these as part of the write-host statement the script throws an error message stating "missing property name after reference operator". Also as I'm using Windows Powershell ISE, as soon as I type the square brackets after the variable $bListItem I get the red squiggly underline suggesting I'm writing invalid code. – Insert_Nickname_Here Aug 30 '16 at 8:39
  • the content of the list item description is a html string, right? You've pointed out that it works if the description is blank. What happens if the description of the list item contains a simple string and not html markup? – Celophysis Aug 30 '16 at 13:33
up vote 0 down vote accepted

I've got it to work, the description field needed to be in brackets/quotes, but without the dot - so it looks like $bListItem["description"] Thank you @Celophysis for your help

  • oh yes, sorry the dot was my mistake :-/ – Celophysis Oct 24 '16 at 8:43

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.