Freigeben über


Copying Lists with PowerShell in SharePoint 2010

While working through an upgrade issue, we had a need to move about 14 Discussion Boards to a subweb.  We initially went the old school route of saving the lists as a template, including the content, then creating lists using the templates.  The content came across, but the Created By fields were showing the user that created the list from the template.

I then remembered that in SharePoint 2010, you can export and import lists.  There is UI in Central Admin to export a list, but there is no UI to import the list.  With 14 lists, manually exporting/importing the lists wasn’t real appealing, but a bit of research turned up the following PowerShell cmdlets:  Export-SPWeb and Import-SPWeb.

With the PowerShell cmdlets, I put together the following PowerShell script that takes in a comma-delimited set of List Titles, and copies them to another web.  The key part to getting the Created By and Modified By info to come across with the list is the –IncludeUserSecurity switch with the cmdlets.

$sourceWebUrl

Web containing the lists to copy

$destWebUrl

Destination web to copy the lists to

$path

Path to create the export files to

$lists

Comma delimited set of List Titles to copy
    1: #This is the source web that is hosting the lists to move
    2: $sourceWebUrl = "https://server.SharePoint.Com/Sub1"
    3:  
    4: #This is the destination web, where the lists will be copied to
    5: $destWebUrl = "https://server.SharePoint.com/Sub1/forums"
    6:  
    7: #Location to store the export file
    8: $path = "\\Server\Share\"
    9:  
   10: #comma delimited list of List Names to copy
   11: $lists = @("List Number 1", "List Number 2")
   12:  
   13:  
   14: #Loop through the lists, export the list from the source, and import the list into the destination
   15: foreach($list in $lists)
   16: {
   17:     "Exporting " + $sourceWebUrl + "/lists/" + $list
   18:  
   19:         export-spweb $sourceWebUrl -ItemUrl ("lists/" + $list) -IncludeUserSecurity -IncludeVersions All -path ($path + $list + ".cmp") -nologfile
   20:  
   21:     "Exporting complete."
   22:  
   23:  
   24:  
   25:     "Importing " + $destWebUrl + "/lists/" + $list
   26:  
   27:         import-spweb $destWebUrl -IncludeUserSecurity -path ($path + $list + ".cmp") -nologfile
   28:  
   29:     "Importing Complete"
   30:     "`r`n`r`n"
   31: }

Comments

  • Anonymous
    September 13, 2011
    good information  the way of written was appriciated sharepointsolution2010.blogspot.com/.../import-and-export-sites-on-sharepoint.html

  • Anonymous
    October 02, 2011
    nice PowerShell snippet. thanks for sharing.

  • Anonymous
    August 17, 2012
    Excellent walk through.

  • Anonymous
    December 11, 2012
    nice PowerShell snippet. thanks for sharing.

  • Anonymous
    November 26, 2013
    November 2013 - Still helping, thanks for posting this.

  • Anonymous
    September 15, 2014
    September 2014 — STILL helping! :) This is the best solution I've found, and I've tried a few. Thank you.

  • Anonymous
    October 27, 2014
    Does the list keep its original GUID?

  • Anonymous
    November 27, 2014
    nice post. another helpful post

  • Anonymous
    December 16, 2014
    It's working like charm. Thanks a lot.