Copy List Items Between SharePoint Lists using PowerShell
Requirement: Copy List Items Between SharePoint Lists using PowerShell
One of our SharePoint list, called “News,” overgrown and wanted to archive its contents to another list periodically.
Solution: So the task is to copy list items between SharePoint Lists. Let’s achieve it using PowerShell.
PowerShell Script to copy List Items:
Would you like to copy items between two SharePoint lists? Perhaps you have a list of test items, and you would like to migrate data from one list to another without manually entering all the data. This article will show you copying items between SharePoint lists is easy using PowerShell.
#Configuration variables
$WebURL = "https://intranet.crescent.com"
$SourceListName = "News"
$TargetListName= "NewsArchive"
#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]
#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();
#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
$TargetItem = $TargetList.AddItem()
Foreach($column in $SourceColumns)
{
if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
{
$TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
}
}
$TargetItem.Update();
}
Copy Attachment between Lists:
If you want to copy list items with attachments, use this script:
#Configuration variables
$WebURL = "https://intranet.crescent.com"
$SourceListName = "News"
$TargetListName= "NewsArchive"
#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]
#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();
#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
$TargetItem = $TargetList.AddItem()
Foreach($column in $SourceColumns)
{
if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
{
$TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
}
}
$TargetItem.Update()
#Copy Attachments
Foreach($Attachment in $SourceItem.Attachments)
{
$spFile = $SourceList.ParentWeb.GetFile($SourceItem.Attachments.UrlPrefix + $Attachment)
$TargetItem.Attachments.Add($Attachment, $spFile.OpenBinary())
}
}
What if the column names are different?
$SourceItems = $SourceList.items
foreach ($SourceItem in $SourceItems)
{
write-host -foregroundcolor yellow Copying Item: $SourceItem["Title"]
$TargetItem = $TargetList.AddItem()
$TargetItem["Title"] = $SourceItem["Title-Column"]
$TargetItem["Another-Field"] = $SourceItem["Another-Field"]
$TargetItem.update()
}
Hi, do you have this Script available to copy single columns between lists? I just need to transfer one column between lists, not copy the whole list. Thanks!
Here you go:
Copy SharePoint List Column Values from One to Another using PowerShell
Hi, the link you added just shows how to copy single columns within the same list, but i need to copy 1 Column from List A to List B. Is that possible with a script?
does this work with SPO List?
For SharePoint Online, use SharePoint Online: Copy List Items from One List to Another using PowerShell
I am receiving error “Exception calling “Update” with “0” argument(s): “To add an item to a document library, use SPFileCollection.Add()” ” Please help!
I’m getting the same error. Did you ever resolve this issue?
Thank you for the update!