Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" $login= "admin@mycompany.onmicrosoft.com" # Office 365 account name $password = "password" # Office 365 password $targetSite="https://mycompany.sharepoint.com/sites/targetsite" # URL of the site containing the target list or library $targetFieldInternalName="InternalFieldName" # Internal name of the target lookup field without spaces $targetFieldTitle="FieldName" # Title of the target lookup field $targetList="TargetListName" # Title of the target list or library $lookupSite="https://mycompany.sharepoint.com/sites/lookupsite" # URL of the site containing the lookup list or library $lookupList="LookupListName" # Title of the lookup list or library Try { $credentials= New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($login,(ConvertTo-SecureString $password -AsPlainText -Force)) # If you're using SharePoint on-premises, uncomment the line below: # $credentials = New-Object System.Net.NetworkCredential($login, (ConvertTo-SecureString $password -AsPlainText -Force)) $lCtx= New-Object Microsoft.SharePoint.Client.ClientContext($lookupSite) $lCtx.Credentials = $credentials $tCtx= New-Object Microsoft.SharePoint.Client.ClientContext($targetSite) $tCtx.Credentials = $credentials # Getting Web Id and List Id of the lookup list $lList = $lCtx.Web.Lists.GetByTitle("$lookupList") $lWeb = $lCtx.Web $lCtx.Load($lWeb) $lCtx.Load($lList) $lCtx.ExecuteQuery() $lookupListID= $lList.Id $lookupWebID=$lWeb.Id # Getting the target lookup field from the target list $tList = $tCtx.Web.Lists.GetByTitle($targetList) $tFields = $tList.Fields $tCtx.Load($tFields) $tCtx.ExecuteQuery() $field = $tFields | where { ($_.Internalname -eq $targetFieldInternalName) -or ($_.Title -eq $targetFieldTitle) } if ($field -eq $null) { throw "A lookup field doesn't exist in a target list" } if ($field.Length -gt 1) { throw "Delete a field duplicate from the target list before proceeding" } # Update schema of a target field $field.SchemaXml = $field.SchemaXml.Replace($field.LookupWebId.ToString(), $lookupWebID.ToString()) $field.SchemaXml = $field.SchemaXml.Replace($field.LookupList.ToString(), $lookupListID.ToString()) $field.Update() $tCtx.ExecuteQuery() Write-host "Lookup field has been repaired successfully" -ForegroundColor Green } Catch { write-host "$($_.Exception)" -foregroundcolor red }