How to: Clone a list using SharePoint power shell

In this short post I want to give you an example of how you can clone a SharePoint list using a SharePoint power shell script.

Here is the script:

# get web 
$web = Get-SPWeb http://yoursite
# get list
$list = $web.Lists["Your List"]
# save list as a template without content
$list.SaveAsTemplate($list.ID.ToString(), $list.ID.ToString(), "", $false)
# get the template from the custom list templates
$template = $web.Site.GetCustomListTemplates($web)[$list.ID.ToString()]
# clone list using the template
$archive = $web.Lists.Add($list.Title + "_Archive", "Archive for the list " + $list.Title, $template);
# get web 
$web = Get-SPWeb http://yoursite
# get list
$list = $web.Lists["Your List"]
# save list as a template without content
$list.SaveAsTemplate($list.ID.ToString(), $list.ID.ToString(), "", $false)
# get the template from the custom list templates
$template = $web.Site.GetCustomListTemplates($web)[$list.ID.ToString()]
# clone list using the template
$archive = $web.Lists.Add($list.Title + "_Archive", "Archive for the list " + $list.Title, $template);
  1. Save the list as a template without content
  2. Create a new list using the template

I used this script to create an archive for this list. In this case I also had to remove all the item event receivers from the copied list.

# remove all event receivers from the cloned list
ForEach($r in $($archive.EventReceivers))
{
    $r.Delete()
}
# remove all event receivers from the cloned list
ForEach($r in $($archive.EventReceivers))
{
    $r.Delete()
}

Learn how to copy your list to a different SharePoint site or site collection.

If you are interested, here is an example of how you can archive items using a SharePoint Designer Workflow.