SharePoint Online: Copy Values from One Column to Another using PowerShell

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

PowerShell to Copy List Column Value in SharePoint Online

To copy values between columns in the same list, use this PnP PowerShell script:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$SourceColumn = "Project_x0020_Name" #Internal Name of the Fields
$DestinationColumn = "ProjectName"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get all items from List
$ListItems = Get-PnPListItem -List $Listname

#Copy Values from one column to another
ForEach ($Item in $ListItems) 
{
    Set-PnPListItem -List $Listname -Identity $Item.Id -Values @{$DestinationColumn = $Item[$SourceColumn]}
}

For field types like single lines of text or choice, this script works fine. However, we have to do some more work for another field type, such as hyperlink, person or group, etc.

SharePoint Online: Copy Values from One Field to Another using PowerShell

Here is how to copy data from one column to another. Make sure the new column is of the same type.

#Function to copy values from one column to another in a list
Function Copy-SPOListColumnValues()
{
    Param
    (
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $SourceColumnName,
        [Parameter(Mandatory=$true)] [string] $TargetColumnName
    )
    Try {
        #Get All Items from the Source List in batches 
        Write-Progress -Activity "Reading Source..." -Status "Getting Items from Source List. Please wait..."
        $ListItems = Get-PnPListItem -List $ListName -PageSize 2000
         Write-host "Total Number of Items Found:"$ListItems.count
 
        #Get fields to Update from the Source List - Skip Read only, hidden fields, content type and attachments
        $SourceField = Get-PnPField -List $ListName -Identity $SourceColumnName
        $TargetField = Get-PnPField -List $ListName -Identity $TargetColumnName

        #Loop through each item in the source and Get column values, add them to target column
        [int]$Counter = 1
        ForEach($ListItem in $ListItems)
        {  
            $ItemValue = @{}
            #Check if the Field value is not Null
            If($ListItem[$SourceField.InternalName] -ne $Null)
            {
                #Handle Special Fields
                $FieldType  = $SourceField.TypeAsString
 
                If($FieldType -eq "User" -or $FieldType -eq "UserMulti" -or $FieldType -eq "Lookup" -or $FieldType -eq "LookupMulti") #People Picker or Lookup Field
                {
                    $LookupIDs = $ListItem[$SourceField.InternalName] | ForEach-Object { $_.LookupID.ToString()}
                    $ItemValue.add($TargetField.InternalName,$LookupIDs)
                }
                ElseIf($FieldType -eq "URL") #Hyperlink
                {
                    $URL = $ListItem[$SourceField.InternalName].URL
                    $Description  = $ListItem[$SourceField.InternalName].Description
                    $ItemValue.add($TargetField.InternalName,"$URL, $Description")
                }
                ElseIf($FieldType -eq "TaxonomyFieldType" -or $FieldType -eq "TaxonomyFieldTypeMulti") #MMS
                {
                    $TermGUIDs = $ListItem[$SourceField.InternalName] | ForEach-Object { $_.TermGuid.ToString()}                    
                    $ItemValue.add($TargetField.InternalName,$TermGUIDs)
                }
                Else
                {
                    #Get Source Field Value and add to Hashtable
                    $ItemValue.add($TargetField.InternalName,$ListItem[$SourceField.InternalName])
                }
                Write-Progress -Activity "Copying List Items:" -Status "Copying Item ID '$($ListItem.Id)' from Source List ($($Counter) of $($ListItems.count))" -PercentComplete (($Counter / $ListItems.count) * 100)

                #Copy column value from source to target
                Set-PnPListItem -List $ListName -Identity $ListItem -Values $ItemValue | Out-Null
 
                Write-Host "Copied Values from Source to Target Column of Item '$($ListItem.Id)' ($($Counter) of $($ListItems.count)) "
                $Counter++
            }
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message 
    }
}

#Set Parameter values 
$SiteURL  = "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "ProjectTracking"
#Source and Target column Internal Names
$SourceColumnName = "ProjectTeam"
$TargetColumnName = "TeamMembers"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  #-Interactive

#Call the Function to copy sharepoint list column values from one to another 
Copy-SPOListColumnValues -ListName $ListName -SourceColumnName $SourceColumnName -TargetColumnName $TargetColumnName

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!

One thought on “SharePoint Online: Copy Values from One Column to Another using PowerShell

  • hey,
    i want to get image field value. the url of image.how its possible?
    thanks

    Reply

Leave a Reply

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