Powershell Week – Day 2: Copying Lists

The second post of the Powershell Week is about copying lists. I found this to be very interesting when you have multiple copies of a environment (for several developers, for instance) and needs to synchronize the data structure and content between them.

I based these 2 scripts on the article Copying Lists with PowerShell in SharePoint 2010 from Jerry Orman.

I created 2 scripts:

One to export the data

$sourceWebUrl = "http://yourwebapplicationurl.com"

# path to save the exported data
$path = "c:\data\export\"

#comma delimited list of List names to copy
$lists = @("List1", "List2")

#comma delimited list of Document Libraries to copy
$libs = @("DocLib1", "DocLib2")

foreach($list in $lists)
{
    "Exporting List " + $list
    export-spweb $sourceWebUrl -ItemUrl ("/lists/" + $list) -IncludeVersions All -path ($path + $list + ".cmp") -force
    "Exporting complete."
}

foreach($lib in $libs)
{
    "Exporting Document Library " + $lib
    export-spweb $sourceWebUrl -ItemUrl ("/" + $lib) -IncludeVersions All -path ($path + $lib + ".cmp") -force
    "Exporting complete."
}

And one to import the data.

$destWebUrl = "http://yourwebapplicationurl.com"

# path to load data from
$path = "c:\data\import\"

#comma delimited list of List names to copy
$lists = @("List1", "List2")

#comma delimited list of Document Libraries to copy
$libs = @("DocLib1", "DocLib2")

foreach($list in $lists)
{
   "Importing List " + $list
   import-spweb $destWebUrl -path ($path + $list + ".cmp") -force
   "Importing complete."
}

foreach($lib in $libs)
{
   "Importing Document Library " + $lib
   import-spweb $destWebUrl -path ($path + $lib + ".cmp") -force
   "Importing complete."
}

I hope you find it useful as well.

See you,
Amadeu.

Advertisements