Skip to main content
SysAdmin

Copy Sharepoint list items to another list with Powershell and PnP

By April 8, 2023No Comments

You can easily copy Sharepoint list items to another list with Powershell and PnP.

First, connect to the source Sharepoint site with PnP-Online:

1.  #Create credential object
2.  $cred = Get-Credential
3.
4.  #Import the Skype for Business Online PS session
5.  Connect-PnPOnline -url $siteUrl -Credentials $cred

Get the list items

Now we need to retrieve the list of items that we want to copy:

1.  # Get source list items
2.  $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)

1.  #Connect to destination site using the connector script
2.  Connect-PnPOnline -url 'http://conto.sharepoint.com/site'
3.
4.  foreach($item in $listItems) {
5.          #Create object
6.    $itemVal = @{
7.      'ProjectName' = $item['ProjectName']
8.      'Title' = $item['Title']
9.      'Desciption' = $item['Desciption']
10.     'Address' = $item['Address']
11.   }
12.   Add-PnPListItem -List 'newlist' -Values $itemVal -ContentType "Item"
13. }

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

1.  # Get all the lists from the connected Sharepoint site
2.  Get-pnplist
3.
4.  # Show fields from the selected list
5.  Get-pnpfield -list 'listname'

If one of the columns in the list contains a hyperlink, then you can get the content from it with:

1.  $url = $item['HyperlinkFieldName'].url
2.  $desc = $item['HyperlinkFieldName'].description
3.
4.  $itemVal = @{
5.    'NewLinkField' = "$url, $desc"
6.    'Title' = $item['Title']
7.  }