0

The below PowerShell I was able to copy all items from a list to another list but I am missing the metadata in the destination list. How to keep the Metadata when copying the list. Please help.

#Get Objects $WebURL = "http://"

$web = Get-SPWeb $WebURL

$SourceListName = ""

$TargetListName= ""

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

}

2 Answers 2

1

We suggest you use "Save as Template" feature to meet your needs.

1.Go to List settings of the source site >> Save list as template

enter image description here

2.On the Save as Template page, please select "Include Content" option.

enter image description here

3.Go to Site settings of the source site >> List templates, you will see the list template you just created.

enter image description here

4.If you want create list in same site, please following steps:

  • a.Go to Site Content >> add an app >> find and click the list template and create new list

enter image description here

  • b.Go to the new list, you will find all items are copied to the new list.

enter image description here

5.If you want create list in another site, please following steps:

  • a.Download the template of the source site

enter image description here

  • b.Upload the template on the destination site

enter image description here

  • c.Go to Site Content of the destination site >> add an app >> find and click the list template and create new list

enter image description here

  • d.Go to the new list, you will find all items are copied to the new list.

enter image description here

========================= Updated Answer =======================

1.Both lists should have same columns

enter image description here

2.Please run the below PowerShell script as an admin

Add-PSSnapin Microsoft.Sharepoint.Powershell
#Configuration variables
$WebURL = "http://sp/sites/sitename/"
$SourceListName = "listnameA"
$TargetListName= "listnameB"
#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();
}

enter image description here

4
  • 1
    Not a solution since the list is very big I have to use PowerShell.
    – Eric
    Jun 15, 2021 at 13:03
  • Hi Eric. I have updated answer under initial My Answer. Hope this can help you to solve issue. Have a nice day! Jun 16, 2021 at 9:26
  • I did create the new list from Template. The PowerShell script will copy all data and metadata except "created" and "created by" not correct, so created column will show today's date for all records.
    – Eric
    Jun 16, 2021 at 13:12
  • Hi Eric. This is normal. Because using PowerShell to copy items to another list is equivalent to creating new items in the new list, so "Created" and "Created By" will be different with old list. Jun 17, 2021 at 3:27
0

The before mentioned script, which can be seen on the sharepointdiary worked for me.

There is a big Important Notice at the start of the article. "Both lists should have same columns! So, have the source list saved as template and create a new list from it". Have you checked that all internal column names are the Same?

You could use @EchoDu_MSFT answer to replicate the list and leave out the list items and move over the list items to the new list, afterwards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.