#Get the source and destination sites (SPWeb objects)
$site = New-Object Microsoft.SharePoint.SPSite(
"http://goodfoods/marketing"
)
$web = $site.OpenWeb()
$destinationSite = New-Object Microsoft.SharePoint.SPSite(
"http://goodfoods/promotions"
);
$destinationWeb = $destinationSite.OpenWeb()
#Define the source and destination list names
$SourceListName =
"Health Foods"
;
$DestinationListName =
#Connect to the source list
$sourceList = $web.Lists[$SourceListName];
#Create a unique name to use when saving the list as a template.
$id = [Guid]::NewGuid()
$templateName = [String]::Format(
"{0}-{1}"
,$sourceList.Title,$id.ToString());
$templateFileName = $templateName;
#Save the list as a list template. The forth parameter of the SaveAsTemplate method takes a boolean value indicating whether to save list data with the list.
$sourceList.SaveAsTemplate($templateFileName, $templateName, $sourceList.Description, $
true
#Get the list template that was just saved
$listTemplate = $site.GetCustomListTemplates($web)[$templateName]
#Create a new list at the destination web using the list template created from the source list
$destinationWeb.Lists.Add($destinationListName, $sourceList.Description, $listTemplate);
$destinationWeb.Update()
#Clean Up
#Delete the list the list template for, the List Template Fallery
$listTemplates = $site.RootWeb.Lists[
"List Template Gallery"
]
$lt = $listTemplates.Items | ?{$_.Title -eq $templateName}
if
($lt -ne $
null
){$lt.Delete();}
#Dispose of the SPWeb and SPSite objects
$web.Dispose();
$site.Dispose();
$destinationWeb.Dispose();
$destinationSite.Dispose();
function Copy-SPList{
################################################################
#.Synopsis
# Copies a list or document library from one web in a site collection to another web in the same site collection
#.DESCRIPTION
# Use this function to copy a list or document library and all of its items to a new list in the same web or a different web. You can only copy list and document libraries between webs in same site collection.
#.Parameter SourceWebUrl
# The full url to the web that hosts the list that will be copied
#.Parameter SourceListName
# The list title of the list that will be copied
#.Parameter DestinationWebUrl
# The full url to the web where the list will be copied to
#.Parameter DestinationListName
# The name given to the list created at the destination web. If this is omitted, the source list name will be used.
#.EXAMPLE
# Copy-SPList -SourceWebUrl http://corporate -SourceListName "SecretDocuments" -DestinationWebUrl http://corporate/moresecureweb
# Copy the SecretDocuments document library from the http://corporate web to the http://corporate/moresecure web, keeping the same list name.
# Copy-SPList -SourceWebUrl http://corporate -SourceListName "SecretDocuments" -DestinationWebUrl http://corporate/lesssecureweb -DestinationListName "NotSoSecretDocuments"
# Copy the SecretDocuments document library from the http://corporate web to the http://corporate/lesssecure web, changing the name of the list to "NotSoSecretDocuments".
# Copy-SPList -SourceWebUrl http://corporate -SourceListName "SecretDocuments" -DestinationWebUrl http://corporate -DestinationListName "ACopyOfTheSecretDocuments"
# Create a copy the SecretDocuments document library in the same web, http://corporate, with the new name "ACopyOfTheSecretDocuments".
[CmdletBinding()]
Param(
[parameter(Mandatory=$
)][
string
]$SourceWebUrl,
]$SourceListName,
]$DestinationWebUrl,
false
]$DestinationListName
$numberOfActions = 6;
Write-Progress -Id 1 -ParentId 0 -Activity
"Copying list from site $SourceWebUrl to $DestinationWebUrl"
-PercentComplete (1/$numberOfActions *100) -Status
"Connecting to the sourcen web, $SourceWebUrl."
$site = New-Object Microsoft.SharePoint.SPSite($SourceWebUrl)
-PercentComplete (2/$numberOfActions *100) -Status
"Connecting to the destination web, $DestinationWebUrl."
$destinationSite = New-Object Microsoft.SharePoint.SPSite($DestinationWebUrl);
Try
{
-PercentComplete (3/$numberOfActions *100) -Status
"Getting the source list, $SourceListName."
$destinationListDescription = $sourceList.Description;
-PercentComplete (4/$numberOfActions *100) -Status
"Saving the source list as a temmplate."
([String]::IsNullOrEmpty($DestinationListName)){$DestinationListName = $SourceListName;}
-PercentComplete (5/$numberOfActions *100) -Status
"Creating a new list ($DestinationListName) in the $DestinationWebUrl site."
$destinationWeb.Lists.Add($destinationListName, $destinationListDescription, $listTemplate) |
out
-
write-host
"The list $SourceListName has been copied to $DestinationWebUrl"
-foregroundcolor Green
-PercentComplete (6/$numberOfActions *100) -Status
Sleep 3;
}
Catch
"Failed to copy the list $SourceListName"
Sleep 3
Write-Host
"An error occurred: $_"
Finally
($web -ne $
){$web.Dispose()}
($site -ne $
){$site.Dispose()}
($destinationWeb -ne $
){$destinationWeb.Dispose()}
($destinationSite -ne $
){$destinationSite.Dispose()}
Import-Module C:\Scripts\CopySPList.ps1
get
-help Copy-SPList -Detailed
Copy-SPList -SourceWebUrl
"http://devmy101"
-SourceListName
"SecretDocuments"
-DestinationWebUrl
"http://devmy101/moresecure"
"http://devmy101/lesssecure"
-DestinationListName
"NotSoSecretDocuments"
"ACopyOfTheSecretDocuments"
Matthew Yarlett edited Revision 16. Comment: Added extra information about how the script works to the Introduction section.
Gokan Ozcifci edited Revision 12. Comment: Formatting
Patris_70 edited Revision 11. Comment: deleted (en-US) title
Craig Lussier edited Revision 10. Comment: added en-US to tags and title
Craig Lussier edited Revision 8. Comment: minor edit to html - toc did not pick up the Introduction section
Craig Lussier edited Revision 7. Comment: Modified headings, moved toc and added link in the Intro section. Also added has code tag.
Craig Lussier edited Revision 6. Comment: minor edit to the post title
Craig Lussier edited Revision 5. Comment: minor edit - modified heading
Craig Lussier edited Revision 4. Comment: Added toc and see also heading referencing the C# wiki post
Craig Lussier edited Revision 2. Comment: added 'SharePoint' tag
Craig Lussier edited Revision 1. Comment: minor formatting edits
Thanks for sharing this script. Cheers.
...in addition, you should consider posting this script to the TechNet Gallery