You can easily copy Sharepoint list items to another list with Powershell and PnP.
First, connect to the source Sharepoint site with PnP-Online:
#Create credential object $cred = Get-Credential #Import the Skype for Business Online PS session Connect-PnPOnline -url $siteUrl -Credentials $cred
Get the list items
Now we need to retrieve the list of items that we want to copy:
# Get source list items $listItems = Get-PnPListItem -List 'ListName' -Fields "Project","Title","Description","Address"
Copy the items to the destination list
Now it’s time to copy the data. First, connect to the destination site (if the list is at another Sharepoint site)
#Connect to destination site using the connector script Connect-PnPOnline -url 'http://conto.sharepoint.com/site' foreach($item in $listItems) { #Create object $itemVal = @{ 'ProjectName' = $item['ProjectName'] 'Title' = $item['Title'] 'Desciption' = $item['Desciption'] 'Address' = $item['Address'] } Add-PnPListItem -List 'newlist' -Values $itemVal -ContentType "Item" }
A few important notes (that took me a few hours to figure out)
In Sharepoint, list columns can have a different internal name than you see when open Sharepoint. To get all the available list names, run the following cmdlet
# Get all the lists from the connected Sharepoint site Get-pnplist # Show fields from the selected list Get-pnpfield -list 'listname'
If one of the columns in the list contains a hyperlink, then you can get the content from it with:
$url = $item['HyperlinkFieldName'].url $desc = $item['HyperlinkFieldName'].description $itemVal = @{ 'NewLinkField' = "$url, $desc" 'Title' = $item['Title'] }
Hey Ruud, maybe update this article to the latest PnP Version so people don’t get stuck when executing the commands.
User Connect-PnPOnline -Url ‘TargetURL’ -Interactive
instead of
ConnectTo-Sharepoint -siteUrl ‘http://conto.sharepoint.com/site’
Does the script work if the source list contains columns that are choice fields or indeed any other field type? Also I notice you don’t pre-create the list before adding each item , does this mean the list will be created automatically?
I don’t know, it’s a couple of year ago that I wrote this script.
Just for your info, before the Foreach loop, I had to create a provisioning template using the PnP.PowerShell module commandlet: Export-PnPListToSiteTemplate -Out template.xml -List $list which creates template for the list definition. Then run Invoke-PnPSiteTemplate -Path template.xml to actually create the blank list in the destination site. Thanks for for the writing the post.
Great article Rudy. Quick question, do you know how to transfer V3Comment history with PnP? I can transfer the latest comment from a list with your script, but I haven’t been able to find a way to do the previous comments.
Hi thank you for the post. Unfortunately, when i use the Get-PnPListItem -List ‘ListName’ -Fields “MyField”, the value of the filed is empty. And when i use this (Get-PnPListItem -List Tasks -Fields “MyField”).FieldValues, i get all the fileds and no way to filter for just “MyField”. Hopefully soemone will read this and have an idea for me :).
Nice article, how do i access list from subsite ?
Just simply connect to the sub site:
https://contoso.sharepoint.com/teams/awesome-test-group/awesome-sub
Yes that worked perfectly fine. Also thank you for mentioning about the internal column names, that saved my time. Thanks again.