I want to compare some items in 2 Lists:

Update item "Company Name" in List 2 when it not match with item "Existing Company" in List 1.

I don't know how to proceed!

  • How do you match two items? Do they have the same name? – helb Nov 9 '18 at 13:25
  • I have a workflow that copy the value from the item in List 1 in item of List 2. Now I want to Update this items. – Jihen Nov 9 '18 at 13:47

It is not quite clear how You decide (based on what properties) which items from "List 2" needs to be updated.

But in general it seems that You could use this script - please adjust based on Your system values (webs URL, list names, field internal names etc).

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) { Add-PSSnapin Microsoft.SharePoint.Powershell }

$web = Get-SPWeb "https://www.contoso.com"

$list1 = $web.Lists["List Name 1"]
$list2 = $web.Lists["List Name 2"]

foreach($item1 in $list1.Items)
    $searchValue = $item1["Title"]

    $query = New-Object Microsoft.SharePoint.SPQuery
    $query.ViewAttributes = "Scope='Recursive'"
    $query.Query = "<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>" + $searchValue + "</Value></Contains></Where>"
    $query.RowLimit = 10    
    # leave value 10 to catch if multiple items are found OR change to 1 and remove "if-else-else" if you are sure for unique matching

    $items2 = $list2.GetItems($query)

    if($items2.Count -eq 1)
        # if values are unique, item collection "$items2" should contain only one item 
        $item2 = $items2[0]

        $item2["Field 1"] = $item1["Other Field"]
        $item2["Field 2"] = "lorem ipsum"

    else if ($items2.Count -eq 0)   
        Write-host "# No matching items found for value:" + $searchValue 
        Write-host "# Multiple matching items found for value:" + $searchValue 

Write-Host "End" -b DarkGreen -f Green
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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