Friday, January 24, 2025

How to Duplicate a SharePoint List Using PnP PowerShell

When working with SharePoint, you might find yourself in situations where you need to create a duplicate of an existing list. This could be for backup purposes, testing, or simply as a template for new content. Fortunately, using PnP PowerShell, you can automate this process in just a few easy steps.

In this blog post, I'll walk you through how to create a duplicate of a SharePoint list, including its structure and items, using a simple PowerShell script with PnP PowerShell commands.


What is PnP PowerShell?

PnP PowerShell is a powerful library of cmdlets that helps you interact with SharePoint and Office 365. It allows you to manage SharePoint Online or SharePoint Server environments from the command line, automating common tasks and simplifying your SharePoint administration.


Prerequisites:

Before we dive into the script, make sure you have the following:

  1. PnP PowerShell module installed. If you haven't installed it yet, you can do so by running this command:

    Install-Module -Name PnP.PowerShell -Force -AllowClobber
  2. Permissions: Ensure you have appropriate permissions to access the source list and create new lists on the SharePoint site.


Steps to Duplicate a SharePoint List Using PnP PowerShell:

1. Connect to Your SharePoint Site

The first step is to connect to your SharePoint site where the source list is located. This can be done using the Connect-PnPOnline cmdlet.

Connect-PnPOnline -Url "https://yoursharepointsiteurl" -UseWebLogin

Replace "https://yoursharepointsiteurl" with your actual SharePoint site URL. You will be prompted to log in if necessary.

2. Get the Source List

Next, retrieve the list you want to duplicate. We use the Get-PnPList cmdlet for this.

$sourceList = Get-PnPList -Identity "Source List Name"

Make sure to replace "Source List Name" with the name of the list you wish to copy.

3. Create the New Duplicate List

Now, let's create a new list that will serve as the duplicate of the source list. The new list will have the same template as the original.

New-PnPList -Title "New Duplicate List" -Template $sourceList.Template -Description "Duplicate of the Source List" -OnQuickLaunch $false

In this command:

  • "New Duplicate List" is the title of the new list.
  • The template is copied from the source list to ensure the new list has the same layout.
  • The -OnQuickLaunch $false option means the list won't be added to the Quick Launch bar, but you can adjust this to your preference.

4. Copy Columns (Fields) from the Source List

SharePoint lists often have custom columns (fields) that you may want to duplicate. This step ensures that the new list will have the same columns as the original.

$sourceFields = Get-PnPField -List $sourceList
foreach ($field in $sourceFields) { Add-PnPField -List "New Duplicate List" -DisplayName $field.Title -InternalName $field.InternalName -Type $field.TypeAsString }

This loop retrieves all fields from the source list and adds them to the new list.

5. Copy Items from the Source List to the New List

Next, you'll copy all items (rows of data) from the source list to the new list. This ensures that the new list will contain the same data as the original.

$sourceItems = Get-PnPListItem -List $sourceList
foreach ($item in $sourceItems) { Add-PnPListItem -List "New Duplicate List" -Values $item.FieldValues }

This loop retrieves each item from the source list and adds it to the new list, preserving the field values.

6. Disconnect from the SharePoint Site

Finally, after the operation is complete, disconnect from the SharePoint site.

Disconnect-PnPOnline

Full Script Overview

Here is the complete script for duplicating a SharePoint list:

# Connect to SharePoint site
Connect-PnPOnline -Url "https://yoursharepointsiteurl" -UseWebLogin # Get the source list $sourceList = Get-PnPList -Identity "Source List Name" # Create a new duplicate list New-PnPList -Title "New Duplicate List" -Template $sourceList.Template -Description "Duplicate of the Source List" -OnQuickLaunch $false # Copy columns (fields) to the new list $sourceFields = Get-PnPField -List $sourceList foreach ($field in $sourceFields) { Add-PnPField -List "New Duplicate List" -DisplayName $field.Title -InternalName $field.InternalName -Type $field.TypeAsString } # Copy items from the source list to the new list $sourceItems = Get-PnPListItem -List $sourceList foreach ($item in $sourceItems) { Add-PnPListItem -List "New Duplicate List" -Values $item.FieldValues } # Disconnect from SharePoint site Disconnect-PnPOnline

Conclusion

Duplicating a SharePoint list with PnP PowerShell is a simple and efficient process. With just a few commands, you can easily create an identical list, including its structure and content. This method is particularly useful for bulk operations or for backing up data without having to manually recreate everything.

Have any questions or need further customization for your specific SharePoint environment? Feel free to leave a comment below!

Happy scripting! 😊

No comments:

Post a Comment