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: }