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.

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:

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()
}

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

Leave a Reply

Your email address will not be published. Required fields are marked *