Copy List Items Between SharePoint Lists using PowerShell

Requirement: Copy List Items Between SharePoint Lists using PowerShell
One of our SharePoint list called “News” grown rapidly and wanted to archive its contents to another list periodically.

Copy List Items Between SharePoint Lists using PowerShell

Solution: So the task is to copy list items between SharePoint Lists. Let’s achieve it using PowerShell.

Important: Both lists should have same columns! So, have the source list saved as a template and create a new list from it.

PowerShell Script to copy List Items:

#Configuration variables
$WebURL = "http://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 = "http://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()
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

5 thoughts on “Copy List Items Between SharePoint Lists using PowerShell

Leave a Reply