Copying Items from a SharePoint List to Another List Using PowerShell Script

Yunus Emre Araç
2 min readFeb 24, 2024

--

Referance: https://www.yunusemrearac.com/2024/02/24/sharepoint-listesindeki-ogeleri-powershell-script-kullanarak-baska-bir-listeye-kopyalanmasi/

Hello friends, today I will show you how to copy the items in a list on a sharepoint site to another list on the same site using the powershell script.

For this, we will talk about 3 different situations and what script we will use against these situations.

In our first case, if the columns of our lists are one to one and there is no addition in any item, we can move without any problems with the script below.

$WebURL = "https://portal.yunusemrearac.com"
$SourceListName = "liste1"
$TargetListName= "liste2"

$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]

$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();

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

When using the above script, you will only need to update the site address and list names at the top.

Another of these situations is that these list items have attachments. As you know, since it is not possible to add attachments without creating an item in the list, we need to add the following script to our above PowerShell code, which will also copy the attachments immediately after the update.

Foreach($Attachment in $SourceItem.Attachments)
{
$spFile = $SourceList.ParentWeb.GetFile($SourceItem.Attachments.UrlPrefix + $Attachment)
$TargetItem.Attachments.Add($Attachment, $spFile.OpenBinary())
}

Our last possibility and one of the possible situations is that the column names in the lists are different. In this case, the change we will make is that we will need to manually define the dynamic setting of the column names in our script.

In this section, you need to define the column names by typing the script below where necessary.

$SourceItems = $SourceList.items
foreach ($SourceItem in $SourceItems)
{
write-host -foregroundcolor yellow Copying Item: $SourceItem["Title1"]
$TargetItem = $TargetList.AddItem()
$TargetItem["Title1"] = $SourceItem["Title2"]
$TargetItem["Name1"] = $SourceItem["Name2"]
$TargetItem.update()
}

As you can see above, you can easily move your items from one list to another and thus establish an archive structure.

--

--

Yunus Emre Araç
Yunus Emre Araç

Written by Yunus Emre Araç

Technology Product Manager of Corporate Applications at ING | Old Microsoft Student Partners Lead | İnönü Üniv. Bilg. Müh. | İAU Bilg. Müh. Tezli Yüksek Lisans

No responses yet