How To Copy SharePoint List Data From One List To Other Using PnP PowerShell

Introduction

While moving the configuration lists from Dev/SIT to UAT/Prod, moving the list data is kind of a problem, which we can automate, using PnP-Powershell script.

Please follow the steps given below.
 
Step 1

Connect to Source and Destination tenant.
 
#Pass the UserID and Password to connect the source and destination SharePoint O365 sites. 

Source
  1. $username = "userid1"  
  2. $userPassword = "password1"  
  3. $secpasswd = ConvertTo-SecureString $userPassword -AsPlainText -Force  
  4. $mycreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secpasswd  
Destination
  1. $username1 = "userid2"  
  2. $userPassword1 = "passw0rd2"  
  3. $secpasswd1 = ConvertTo-SecureString $userPassword1 -AsPlainText -Force  
  4. $mycreds1 = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username1, $secpasswd1   
Step 2

Read the list data from the source list and write it in the destination list.
 
Get the Souce context
  1. $webUrl1 = "https://tenant2.sharepoint.com/saurabh/"  
  2. Write-Output $("Connecting to {0}..." -f $webUrl);  
  3. Connect-PnPOnline -Url $webUrl -Credentials $mycreds;  
  4. Write-Output "Context obtained";   
Function to read the data from source list and write it in the destination list
  1. function GetSetListItems() {#  
  2.     Input Parameters  
  3.     $listName = "Configuration List"  
  4.     $fields = "Title""Color_x0020_Code"#  
  5.     Retrieves items  
  6.     $listItems = Get - PnPListItem - List $listName - Fields $fields# Write lists item to other list  
  7.     Connect - PnPOnline - Url $webUrl1 - Credentials $mycreds1;  
  8.     foreach($listItem in $listItems) {  
  9.         $itemValues = @ {  
  10.             "Title" = $listItem["Title"];  
  11.             "Color_x0020_Code" = $listItem["Color_x0020_Code"]  
  12.         }  
  13.         Add - PnPListItem - List $listName - Values $itemValues  
  14.     }  
  15. }   
I hope, this will solve the problem of migrating the lists data from one tenant to other tenant. You can also copy the list data on same tenant. 
Ebook Download
View all
Learn
View all