0

I was able to copy all records from a big list to another list but not able to copy specific records form source list to the destination list using PowerShell as below, Any Help Please.

$web = Get-SPWeb $WebURL

$SourceList = $web.Lists[$SourceListName]

$TargetList = $web.Lists[$TargetListName]

$SourceColumns = $sourceList.Fields

#$SourceItems = $SourceList.GetItems();

$souceItems= $SourceList.items | where {$["created"] -gt '1/1/2015' -and $["created"] -lt '1/1/2020'};

$souceItems.Count;

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

0

Modify your Powershell command to:

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

$SourceColumns = $sourceList.Fields

$caml='<Where>
   <And>
    <Gt>
        <FieldRef Name="Created" />
        <Value IncludeTimeValue="TRUE" Type="DateTime">2015-1-1T12:00:00Z</Value>
    </Gt>
    <Lt>
        <FieldRef Name="Created" />
        <Value IncludeTimeValue="TRUE" Type="DateTime">2020-1-1T12:00:00Z</Value>
    </Lt>
   </And>
   </Where>'
  
$query=new-object Microsoft.SharePoint.SPQuery
$query.Query=$caml | Write-Output

$SourceItems=$Sourcelist.GetItems($query)

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();
}
4
  • I get an error when I run the code: The following exception occurred while trying to enumerate the collection: "One or more field types are not installed properly. Go to the list settings page to delete these fields.". At line:1 char:24 + Foreach($SourceItem in $SourceItems) + ~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemExceptio n + FullyQualifiedErrorId : ExceptionInGetEnumerator
    – Eric
    Jun 8 at 18:22
  • 1. <FieldRef Name=<replace here with the internal name of your column> /> 2. Is your column type Date and Time?
    – Allen_MSFT
    Jun 9 at 8:03
  • Yes I did that and tried different columns, yes the column type is Date and Time but still same error.
    – Eric
    Jun 9 at 13:36
  • Awesome. It worked only when I saved the list as a template and created the Destination List.
    – Eric
    Jun 9 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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