Copy List Items from One list to another using pnp Powershell

In this article we will see how to copy list items from one list to another using PNP PowerShell:

In the below code, just replace the source site url and destination site url. Also, update the columns internal names. I have used -UseWebLogin to login to sites using a web browser, you can also pass credentials manually as shown at the bottom of post.


    Connect-PnPOnline -Url "https://office365notes.sharepoint.com/sites/SourceSite" -UseWebLogin
    $listName = "AssetTypeMaster"  
    $fields = "AssetType", "Description", "AseetTypeDescription"  
    $listItems = Get-PnPListItem -List $listName -Fields $fields # Write lists item to other list 
     
    Connect-PnPOnline -Url "https://office365notes.sharepoint.com/sites/DestinationSite" -UseWebLogin  
foreach($listItem in $listItems) { $itemValues = @{ "AssetType" = $listItem["AssetType"]; "Description" = $listItem["Description"]; "AseetTypeDescription" = $listItem["AseetTypeDescription"] } Add-PnPListItem -List $listName -Values $itemValues }


You can use credentials as well as shown below:

Source:

$username = "userid1"  
$userPassword = "password1"  
$secpasswd = ConvertTo-SecureString $userPassword -AsPlainText -Force  
$mycreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secpasswd 

Destination:

$username1 = "userid2"  
$userPassword1 = "passw0rd2"  
$secpasswd1 = ConvertTo-SecureString $userPassword1 -AsPlainText -Force  
$mycreds1 = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username1, $secpasswd1
Powershell using passing credentials:
$sourceWebUrl = "https://office365notes.sharepoint.com/sites/SourceSite"  
$destinationWebUrl = "https://office365notes.sharepoint.com/sites/DestinationSite"

#Connect to source site  
Connect-PnPOnline -Url $sourceWebUrl -Credentials $mycreds;      
$listName = "AssetTypeMaster"  
$fields = "AssetType", "Description", "AseetTypeDescription"  
 
#Retrieves items  
$listItems = Get-PnPListItem -List $listName -Fields $fields 

#Write lists item to other list  
#Connect to destination site
Connect-PnPOnline -Url $destinationWebUrl -Credentials $mycreds1;  
foreach($listItem in $listItems) {  
    $itemValues = @{  
        "AssetType" = $listItem["AssetType"];  
        "Description" = $listItem["Description"];  
        "AseetTypeDescription" = $listItem["AseetTypeDescription"]  
    }  
    Add-PnPListItem -List $listName -Values $itemValues  
}  

Post a Comment

0 Comments