kwizcom banner advertisement

How to Copy a SharePoint List using PowerShell?

Requirement: Copy a SharePoint List to Another List

PowerShell to Clone a List in SharePoint
While Export-Import is an option, Here is the PowerShell to copy SharePoint list structure using "Save List as Template" and "Create List from Template" methods.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameters
$SiteURL="http://intranet.crescent.com"
$ListName="Projects"
$NewListName = "Projects Archive"
$SaveData= $False

Try {
    #Get the web and List
    $Web = Get-SPWeb $SiteURL
    $List = $Web.Lists[$ListName]

    #Check if the new list doesn't exists
    If($Web.Lists.TryGetList($NewListName) -eq $null)
    {
        #Save list as template
        $List.SaveAsTemplate($List.ID.ToString(), $List.ID.ToString(), "", $SaveData)

        #Get the List template
        $ListTemplate = $web.Site.GetCustomListTemplates($web)[$List.ID.ToString()]

        #Clone list
        $NewList = $web.Lists.Add($NewListName, "$($NewListName)-$($List.Description)", $ListTemplate)

        #Remove the List template file Created
        $ListTemplateFile = $web.Site.RootWeb.GetFolder("_catalogs/lt").Files | where {$_.Name -eq $ListTemplate.InternalName}
        $ListTemplateFile.Delete()

        write-host -f Green "List '$ListName' Cloned to '$NewListName!'"
    }
    Else
    {
        write-host -f Yellow "List '$NewListName' already exists!"
    }

}
Catch {
    write-host -f Red "Error Adding Template to Document Library!" $_.Exception.Message
} 
You can use this script to duplicate SharePoint list with content or just list structure.
How to Copy a SharePoint List using PowerShell? How to Copy a SharePoint List using PowerShell? Reviewed by Salaudeen Rajack on February 10, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.