SharePoint Online: Copy columns from one list to another using PowerShell

Requirement: Copy a column to another list in SharePoint Online.

How to copy columns Between Lists in SharePoint Online?

Have you ever wanted to copy a column from one SharePoint list to another? Maybe you want to use the same column in multiple lists, you want to duplicate data from one list to another, or you wish to move data from one list to another. Whatever the reason, Here are the methods you can use:

Copy Columns in the Web UI by Re-Creating them One by one:

  1. Login to your SharePoint Online site >> Go to the list where you want to copy the column from.
  2. Click on the settings gear icon in the top-right corner, and then select “List settings.”
  3. Under the list settings page, in “Columns.”, find the column you would like to copy, and then click on the column name to open its properties.
  4. Browse to your destination list in the browser in a new tab. Get into its settings, and click on the “Create column” on the settings page.
  5. Start copying the column settings from the source to the destination: E.g., Column name, description, and other settings. Click on OK to create the column finally.copy columns in sharepoint online
  6. Repeat steps 3-5 for each additional column you want to copy.

PowerShell to Copy Columns from One List to Another

Using the web UI to recreate columns could be cumbersome if you have many columns to copy; Luckily, It’s easy to do it with PowerShell script!

#Parameters
$SourceSiteUrl = "https://crescent.sharepoint.com/sites/Retail"  
$DestinationSiteUrl = "https://crescent.sharepoint.com/sites/Sales"
$SourceListName = "Projects"
$DestinationListName = "Projects"
$FieldsToCopy = "ProjectName", "ProjectDescription", "ProjectManager", "Department" #Internal Names

#Connect to the Source site
Connect-PnPOnline -Url $SourceSiteUrl -Interactive
 
#Get All Fields from the Source List
$SourceListFields = Get-PnPField -List $SourceListName

#Connect to Destination site
Connect-PnPOnline -Url $DestinationSiteUrl -Interactive

#Get All Fields from the Desntination List
$DestinationListFields = Get-PnPField -List $DestinationListName

#Copy columns from the Source List to Destination List
ForEach($Field in $FieldsToCopy)
{
    #Check if the destination list has the field already
    $DestinationFieldExist = ($DestinationListFields | Select -ExpandProperty InternalName).Contains($Field)
    If($DestinationFieldExist -eq $false)
    {
        #Get the field to copy
        $SourceField = $SourceListFields | Where {$_.InternalName -eq $Field}
        If($SourceField -ne $Null)
        {
            Add-PnPFieldFromXml -List $DestinationListName -FieldXml $SourceField.SchemaXml | Out-Null
            Write-Host "Copied Field from Source to Destination List:"$Field -f Green
        }
        Else
        {
            Write-Host "Field '$Field' does not Exist in the Source List!" -f Yellow
        }
    }
    Else
    {
        Write-host "Field '$Field' Already Exists in the Destination List!" -f Yellow
    }
}

How about copying all fields except Read-Only, Hidden, and some special fields? Sure! Use:

$FieldsToCopy = Get-PnPField -List $SourceListName | Where {-not ($_.ReadOnlyField) -and (-Not ($_.Hidden)) -and ($_.InternalName -ne  "ContentType") -and ($_.InternalName -ne  "Attachments") } | Select -ExpandProperty InternalName

Please note, that the above methods copy just the columns from one list to another. If you want to copy SharePoint list column values as well, use: How to copy data from one column to another in SharePoint Online list?

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!

Leave a Reply

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