다음을 통해 공유


SharePoint 2010: How to Copy a List Using PowerShell Script

Introduction

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

Other Languages

This article is also available in the following languages: