SharePoint 2010: How to Copy a List Using PowerShell Script

SharePoint 2010: How to Copy a List Using PowerShell Script



Introduction

This script, originally created by Rashu Rahul, was in response to an article he wrote on copying SharePoint lists programmatically within same site collection using C#, How to Copy a List Programmatically in SharePoint 2007 and SharePoint 2010 Within the Same Site Collection Using C#

Use this PowerShell script (function) to copy a list (or document library) and all of its items to a new list in the same web or a different web. You can only copy list and document libraries between webs in same site collection.

The script works by saving the list as a custom list template, including the content. It then creates a new list, based on the template, in the destination web, using the destination list name (if supplied, otherwise the source list name is used).

The Basic PowerShell Code

Using PowerShell, save the list as a custom list template, including the content, then create a new list, based on the template, in the destination web. In the following example, the Health Foods list is copied from the Marketing web to the Promotions web of the GoodFoods site collection.

#Get the source and destination sites (SPWeb objects)
$site = New-Object Microsoft.SharePoint.SPSite("http://goodfoods/marketing")
$web =  $site.OpenWeb()
$destinationSite = New-Object Microsoft.SharePoint.SPSite("http://goodfoods/promotions");
$destinationWeb = $destinationSite.OpenWeb()
 
#Define the source and destination list names
$SourceListName = "Health Foods";
$DestinationListName = "Health Foods";
 
#Connect to the source list
$sourceList = $web.Lists[$SourceListName];
 
#Create a unique name to use when saving the list as a template.
$id = [Guid]::NewGuid()   
$templateName = [String]::Format("{0}-{1}",$sourceList.Title,$id.ToString());
$templateFileName = $templateName;
 
#Save the list as a list template. The forth parameter of the SaveAsTemplate method takes a boolean value indicating whether to save list data with the list.
$sourceList.SaveAsTemplate($templateFileName, $templateName, $sourceList.Description, $true)
 
#Get the list template that was just saved
$listTemplate = $site.GetCustomListTemplates($web)[$templateName]     
 
#Create a new list at the destination web using the list template created from the source list
$destinationWeb.Lists.Add($destinationListName, $sourceList.Description, $listTemplate);
$destinationWeb.Update()
 
#Clean Up
#Delete the list the list template for, the List Template Fallery
$listTemplates = $site.RootWeb.Lists["List Template Gallery"]
$lt = $listTemplates.Items | ?{$_.Title -eq $templateName}
if($lt -ne $null){$lt.Delete();}
 
#Dispose of the SPWeb and SPSite objects
$web.Dispose();
$site.Dispose();
$destinationWeb.Dispose();
$destinationSite.Dispose();


Using the PowerShell as a Reusable Function
The PowerShell code written as a parameterised function that can be reused. It then creates a new list, based on the template, in the destination web, using the destination list name (if supplied, otherwise the source list name is used).
 
function Copy-SPList{
    ################################################################
    #.Synopsis
    #  Copies a list or document library from one web in a site collection to another web in the same site collection
    #.DESCRIPTION
    # Use this function to copy a list or document library and all of its items to a new list in the same web or a different web. You can only copy list and document libraries between webs in same site collection.
    #.Parameter SourceWebUrl
    #  The full url to the web that hosts the list that will be copied
    #.Parameter SourceListName 
    #  The list title of the list that will be copied
    #.Parameter DestinationWebUrl
    #  The full url to the web where the list will be copied to
    #.Parameter DestinationListName
    #  The name given to the list created at the destination web. If this is omitted, the source list name will be used.
    #.EXAMPLE
    #  Copy-SPList -SourceWebUrl http://corporate -SourceListName "SecretDocuments" -DestinationWebUrl http://corporate/moresecureweb
    #  Copy the SecretDocuments document library from the http://corporate web to the http://corporate/moresecure web, keeping the same list name.
    #.EXAMPLE
    #  Copy-SPList -SourceWebUrl http://corporate -SourceListName "SecretDocuments" -DestinationWebUrl http://corporate/lesssecureweb -DestinationListName "NotSoSecretDocuments"
    #  Copy the SecretDocuments document library from the http://corporate web to the http://corporate/lesssecure web, changing the name of the list to "NotSoSecretDocuments".
    #.EXAMPLE
    #  Copy-SPList -SourceWebUrl http://corporate -SourceListName "SecretDocuments" -DestinationWebUrl http://corporate -DestinationListName "ACopyOfTheSecretDocuments"
    #  Create a copy the SecretDocuments document library in the same web, http://corporate, with the new name "ACopyOfTheSecretDocuments".    
    ################################################################
     
    [CmdletBinding()]
    Param(  
            [parameter(Mandatory=$true)][string]$SourceWebUrl,
            [parameter(Mandatory=$true)][string]$SourceListName,
            [parameter(Mandatory=$true)][string]$DestinationWebUrl,        
            [parameter(Mandatory=$false)][string]$DestinationListName
        )
    $numberOfActions = 6;
    Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (1/$numberOfActions *100) -Status "Connecting to the sourcen web, $SourceWebUrl.";    
    $site = New-Object Microsoft.SharePoint.SPSite($SourceWebUrl)
    $web =  $site.OpenWeb()
    Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (2/$numberOfActions *100) -Status "Connecting to the destination web, $DestinationWebUrl.";       
    $destinationSite = New-Object Microsoft.SharePoint.SPSite($DestinationWebUrl);
    $destinationWeb = $destinationSite.OpenWeb()
    Try
    {
        Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (3/$numberOfActions *100) -Status "Getting the source list, $SourceListName.";
        $sourceList = $web.Lists[$SourceListName];
        $id = [Guid]::NewGuid()    
        $templateName = [String]::Format("{0}-{1}",$sourceList.Title,$id.ToString());
        $templateFileName = $templateName;
        $destinationListDescription = $sourceList.Description;
        Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (4/$numberOfActions *100) -Status "Saving the source list as a temmplate.";
        $sourceList.SaveAsTemplate($templateFileName, $templateName, $sourceList.Description, $true)
        if([String]::IsNullOrEmpty($DestinationListName)){$DestinationListName = $SourceListName;}
        $listTemplate = $site.GetCustomListTemplates($web)[$templateName]      
        Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (5/$numberOfActions *100) -Status "Creating a new list ($DestinationListName) in the $DestinationWebUrl site.";
        $destinationWeb.Lists.Add($destinationListName, $destinationListDescription, $listTemplate) | out-null;
        $destinationWeb.Update()
        $listTemplates = $site.RootWeb.Lists["List Template Gallery"]
        $lt = $listTemplates.Items | ?{$_.Title -eq $templateName}
        if($lt -ne $null){$lt.Delete();}
        write-host "The list $SourceListName has been copied to $DestinationWebUrl" -foregroundcolor Green
        Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (6/$numberOfActions *100) -Status "The list $SourceListName has been copied to $DestinationWebUrl";
        Sleep 3;
    }
    Catch
    {
        Write-Progress -Id 1 -ParentId 0 -Activity "Copying list from site $SourceWebUrl to $DestinationWebUrl" -PercentComplete (6/$numberOfActions *100) -Status "Failed to copy the list $SourceListName"
        Sleep 3
        Write-Host "An error occurred: $_"
    }
    Finally
    {
        if($web -ne $null){$web.Dispose()}
        if($site -ne $null){$site.Dispose()}
        if($destinationWeb -ne $null){$destinationWeb.Dispose()}
        if($destinationSite -ne $null){$destinationSite.Dispose()}
    }  
}


 

Importing the Function


Copy the entire function and paste it into a SharePoint Management Shell window (PowerShell). You can now call the function like any other function.

Alternatively, save the function as a Windows PowerShell script (E.g. CopySPList.ps1 ), then import the script into a SharePoint Management Shell window (PowerShell).

Import-Module C:\Scripts\CopySPList.ps1


Examples of Using the Function


Example 1: Get Help

get-help Copy-SPList -Detailed




Example 2: Copy the SecretDocuments document library from the http://devmy101 web to the http://devmy101/moresecure web, keeping the same list name.

Copy-SPList -SourceWebUrl "http://devmy101" -SourceListName "SecretDocuments" -DestinationWebUrl "http://devmy101/moresecure"




Example 3: Copy the SecretDocuments document library from the http://devmy101 web to the http://devmy101/lesssecure web, changing the name of the list to "NotSoSecretDocuments".

Copy-SPList -SourceWebUrl "http://devmy101" -SourceListName "SecretDocuments" -DestinationWebUrl "http://devmy101/lesssecure" -DestinationListName "NotSoSecretDocuments"


Example 4: Create a copy the SecretDocuments document library in the same web, http://devmy101, with the new name "ACopyOfTheSecretDocuments". 

Copy-SPList -SourceWebUrl "http://devmy101" -SourceListName "SecretDocuments" -DestinationWebUrl "http://devmy101" -DestinationListName "ACopyOfTheSecretDocuments"



See Also


Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Matthew Yarlett edited Revision 16. Comment: Added extra information about how the script works to the Introduction section.

  • Gokan Ozcifci edited Revision 12. Comment: Formatting

  • Patris_70 edited Revision 11. Comment: deleted (en-US) title

  • Craig Lussier edited Revision 10. Comment: added en-US to tags and title

  • Craig Lussier edited Revision 8. Comment: minor edit to html - toc did not pick up the Introduction section

  • Craig Lussier edited Revision 7. Comment: Modified headings, moved toc and added link in the Intro section. Also added has code tag.

  • Craig Lussier edited Revision 6. Comment: minor edit to the post title

  • Craig Lussier edited Revision 5. Comment: minor edit - modified heading

  • Craig Lussier edited Revision 4. Comment: Added toc and see also heading referencing the C# wiki post

  • Craig Lussier edited Revision 2. Comment: added 'SharePoint' tag

Page 1 of 2 (11 items) 12
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Craig Lussier edited Revision 1. Comment: minor formatting edits

  • Thanks for sharing this script. Cheers.

  • Craig Lussier edited Revision 2. Comment: added 'SharePoint' tag

  • ...in addition, you should consider posting this script to the TechNet Gallery

  • Craig Lussier edited Revision 4. Comment: Added toc and see also heading referencing the C# wiki post

  • Craig Lussier edited Revision 5. Comment: minor edit - modified heading

  • Craig Lussier edited Revision 6. Comment: minor edit to the post title

  • Craig Lussier edited Revision 7. Comment: Modified headings, moved toc and added link in the Intro section. Also added has code tag.

  • Craig Lussier edited Revision 8. Comment: minor edit to html - toc did not pick up the Introduction section

  • HI Craig,

    Thanks for your comments. I have uploaded this in gallery at gallery.technet.microsoft.com/Powershell-script-to-copy-3890a86f

    Thanks,

    Rahul Rashu

  • Thanks for adding this to the gallery!

  • Very good! thank you !

  • Ed Price - MSFT edited Revision 9. Comment: Title casing

  • Craig Lussier edited Revision 10. Comment: added en-US to tags and title

  • Patris_70 edited Revision 11. Comment: deleted (en-US) title

Page 1 of 2 (18 items) 12