Powershell script to add lookup field item in SharePoint list RRS feed

  • Question

  • Hi,

    I need to import an excel records into SharePoint, each row as a list item. The following script works for all fields other than lookup fields. I have two lookup fields within this list which gets data from a different lists within the site.

    $contents = Import-csv 'C:\excel.csv'

    $list = $web.Lists["SPListname"]

    # Delete  existing List Data, If not required remove this line

    # $list.get_items() | % { $_.Delete() }

    # Iterate for each list column

    foreach ($row in $contents)


          $item = $list.items.add()     

          # Check if cell value is not null

          if ($row.field1 -ne $null)

                {$item["field1"] = $row.field1}     

          if ($row.field2 -ne $null)

                {$item["field2"] = $row.field2}     

          if ($row.field3 -ne $null)

                {$item["field3"] = $row.field3}  




    When it has to insert data into a lookup field, it is failing to do that with the following error message:

    Exception calling "Update" with "0" argument(s): "Invalid data has been used to update the list item. The field you are
     trying to update may be read only."

    What is the best way to add a new record into a list which contains lookup fields.


    Monday, October 21, 2013 1:11 PM


  • ok, my script worked like this for lookup fields:

    if ($row.field -ne $null)


    $lookupfield = $web.lists["lookuplist"].items | where-object {$_.DisplayName -eq $row.field}

    $lookupfield_id = $

    $lookupfield_dn = $lookupfield.displayname

    $item["field"] = $lookupfield_id ;# $lookupfield_dn


    • Marked as answer by svs10 Tuesday, October 22, 2013 1:46 PM
    Tuesday, October 22, 2013 1:46 PM

All replies