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:
PnP PowerShell module installed. If you haven't installed it yet, you can do so by running this command:
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.
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.
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.
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.
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.
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.
Full Script Overview
Here is the complete script for duplicating a SharePoint list:
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! 😊