Introduction
In this article, you will learn how to copy list items from one list to another list using CSOM Powershell on SharePoint. This is mainly focused on using PowerShell scripts for any SharePoint 2013 / SharePoint online sites.
Steps Involved
The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.
- Add the references using the Add-Type command with the necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll.
- Add-Type –Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
- Add-Type –Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- If you are trying to access SharePoint Online site then you need to setup the site credentials with credentials parameter and load it to the client context.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- If you are trying to access the SharePoint on premise site then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.
- #Credentials for on premise site - Start
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)
- $ctx.Credentials = $creds
- #Credentials for on premise site - End
Copy Operation
We will see how we can copy the list items from one list to another list. Here, the list should have identical SharePoint list fields. The values will be copied only if the corresponding fields are present on both source and destination lists.- Using the context, access the web.
- From the web, get the source list and destination list using the list names.
- From the source list, get the items using the query object. The query object should be initiated to access the necessary source list items. In this case, I have used All Items query to get all the items.
- Load and execute the query.
- $list1 = $ctx.Web.Lists.GetByTitle("List1")
- $list2 = $ctx.Web.Lists.GetByTitle("List2")
- $list1Items = $list1.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
- $fields = $list1.Fields
- $ctx.Load($list1Items)
- $ctx.Load($list1)
- $ctx.Load($list2)
- $ctx.Load($fields)
- $ctx.ExecuteQuery()
- For destination list, create list item creation information object and add the object to the destination list.
- Access all the fields using for each loop from the source list.
- For every field, check whether field is not read only field. If it is read only field, then we will not be able to write it to the destination list.
- Also the fields should not be hidden, attachment or content type, since the write operations will not be possible on these fields.
- For other custom fields, execute the copy operations and update the item.
- foreach($item in $list1Items){
- Write-Host $item.ID
- $listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
- $list2Item = $list2.AddItem($listItemInfo)
- foreach($field in $fields){
- #Write-Host $field.InternalName " - " $field.ReadOnlyField
- if((-Not ($field.ReadOnlyField)) -and (-Not ($field.Hidden)) -and ($field.InternalName -ne "Attachments") -and ($field.InternalName -ne "ContentType"))
- {
- Write-Host $field.InternalName " - " $item[$field.InternalName]
- $list2Item[$field.InternalName] = $item[$field.InternalName]
- $list2Item.update()
- }
- }
- }
- $ctx.ExecuteQuery()
Summary
Thus, you have learned how to copy the items from one list to another using CSOM PowerShell on SharePoint 2013 / SharePoint online sites.

vinay ayinPosted Apr 8, 2018, 12:52 PM
Nakkeran, Very good article. Will this work for copying attachments from source to destination?
Pradeep RNPosted Jan 9, 2017, 2:14 AM
Hi Nakkeran this script is going to work only if the field internal names are same in both the lists(source and destination)?
Pradeep RNPosted Dec 22, 2016, 7:57 AM
Is this script can be applied for document library which has folders and subfolders and files with in them??
Pradeep RNPosted Dec 22, 2016, 7:57 AM
Hii Nakeeran a very good article
Bhuvanesh MohankumarPosted May 30, 2016, 1:18 PM
Good one
Vignesh ManiPosted May 30, 2016, 9:34 AM
Nice one
Thiruppathi RPosted May 30, 2016, 6:00 AM
Nice..
Humayun Kabir MamunPosted May 30, 2016, 1:54 AM
Nice...
Debasis SahaPosted May 30, 2016, 1:16 AM
Nice share..