My program must verify this requirement :

Add Owner value from List A where Name (in list A) eq Name (in list B): Here i want to nested the first Foreach in the second one

Add-PSSnapin Microsoft.SharePoint.PowerShell
$web = get-spweb http://intranet/Operations/
$todolist = $web.Lists["ToDo"]
$checkList = $web.Lists["CheckList"]

$Fund = get-spweb http://sites/clients
$PersonsList = $Fund.Lists["Client Allocation"]

$today = Get-Date

foreach ($person in $PersonsList.Items | Where $_["Fund"] -eq "Agenda") 
   $checkListItem = $checkList.Items.Add()

   $checkListItem["Assigned To"]= $Person["Risk Manager In charge"]
   $checkListItem["Backup"] = $Person["Backup"]


foreach ($task in $todolist.Items) 
   $checkListItem = $checkList.Items.Add()

   $checkListItem["StartDate"] = $today.ToShortDateString()
   $checkListItem["DueDate"] = $today.ToShortDateString()
   $checkListItem["Fund Name"] = $task["Fund"].Tostring().Split("|")[0]
   $checkListItem["Task Name"] = $task["Fund"].Label + " -> " + $task["Title"]
   $checkListItem["Link"] = $task["Link"]
   $checkListItem["Details"] = $task["Details"]

   # I want To make here the result of the first Foreach where Title of the checklist eq to the Title Of fund List
   # $checkListItem["Assigned To"] = $Fund.$PersonsList["Risk Manager In charge"]


2 Answers 2


I would go for this pseudo-code - adjust as You need it (different webs, lists and fields, but idea should stay the same).

$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"

$listSource1 = $web.Lists["List Name 1"]
$listSource2 = $web.Lists["List Name 2"]

$listTarget = $web.Lists["List Name 3"]

foreach($item1 in $listSource1.Items)
    $commonValue = $item1["Title"]

    $query = New-Object Microsoft.SharePoint.SPQuery
    $query.ViewAttributes = "Scope='Recursive'"
    $query.Query = "<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>" + $commonValue + "</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 = $listSource2.GetItems($query)

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

        $newItem = $listTarget.Items.Add();

        $newItem["Field 1"] = $item1["Other Field 1"]
        $newItem["Field 2"] = $item1["Other Field 2"]

        $newItem["Field 3"] = $item2["Other Field 3"]
        $newItem["Field 4"] = $item2["Other Field 4"]

    else if ($items2.Count -eq 0)   
        Write-host "# No matching items found for value:" + $commonValue 
        Write-host "# Multiple matching items found for value:" + $commonValue 
Write-Host "End" -b DarkGreen -f Green

Notice: this code will work efficiently if total item count in lists is below hundreds-of-thousands.


you can use below pseudo code

foreach ($task in $todolist.Items) 
    foreach ($chkItem in $checkList.Items) 
        if ($task["Owner"] -eq $chkItem["Owner"]){

            your code to update/insert
  • This code would be highly inefficient - each item in the nested list would be processed half as many times, as there are items in the first list. I would suggest replacing the second "foreach" with "SPQuery" that returns "list item collection" with (hopefully, if all items are unique) only one item --> then use metadata from that item. Aug 17, 2018 at 13:32
  • Thank you for your contribution Sunil & Vilerts. Vilerts can you write us an exemple to use SPQuery in this case?
    – Jihen
    Aug 20, 2018 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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