Replace Content Editor Web Part (CEWP) Links with PowerShell

After a SharePoint Migration with URL change, had to find and replace the links from the SharePoint Content Editor Web Part for all SharePoint sites in a web application. Here is the PowerShell Script to find and replace links from the Content Editor Web Part.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$OldLink="https://sp10.crescent.com"
$NewLink="https://sharepoint2010.crescent.com"

#Get all Webs
$webs = Get-SPWebApplication "https://sharepoint.crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All
 
#Iterate through webs
foreach ($web in $webs)
{
	#Get All Pages from site's Root into $AllPages Array
	$AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})

	#Search All Folders for Pages
	foreach ($folder in $web.Folders)
	{
		if($folder.Name -ne "Forms") #Leave "Forms" Folder
		{
			#Add the pages to $AllPages Array
			$AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
		}
	}

	#Iterate through all pages
	foreach($Page in $AllPages)
	{
		#Web Part Manager to get all web parts from the file
		$WebPartManager = $web.GetLimitedWebPartManager( $Page.ServerRelativeUrl,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

		#Iterate through each web part
		foreach($webPart in $WebPartManager.WebParts) 
		{
			# Get All Content Editor web parts with specific Old Link
			if( ($webPart.Content.InnerText -like '*'+$OldLink+'*' ) -and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) )
			{
				#Get the Old content from CEWPs
				$OldContent =  $webPart.Content
				$OldContentXml = $OldContent.InnerText

				#Replace the Old Links
				$XmlDoc = New-Object System.Xml.XmlDocument
				$NewContentXml= $XmlDoc.CreateElement("content") 
				$NewContentXml.InnerText= $OldContentXml.Replace($OldLink, $NewLink)

				#Set content and Save
				$webpart.Content = $NewContentXml     

				$webPartManager.SaveChanges($webPart);
				Write-Host "Replaced a link in $($Page.ServerRelativeUrl))"
			}
		}
	}
}

Update Content Editor content in Publishing Sites:
We’ll have to call Check-Out, Check-in methods on publishing sites. Here is the Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL ="https://portal.crescent.com"
$OldLink="https://sp10.crescent.com"
$NewLink="https://sharepoint2010.crescent.com"

#Get the web
$Web = Get-SPWeb $WebURL 
#Get Publishing Web
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
#Get Pages Library
$PublishingPages = $PublishingWeb.GetPublishingPages()
 
#Iterate throgh each page
foreach ($Page in $PublishingPages)
{   
#Checkout the page
$Page.CheckOut() 

#Web Part Manager to get all web parts from the file
$WebPartManager = $web.GetLimitedWebPartManager($Page.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  
 #Iterate through each web part
 foreach($webPart in $WebPartManager.WebParts) 
  {
    #Get All Content Editor web parts with specific Old Link
    if( ($webPart.Content.InnerText -like '*'+$OldLink+'*' ) -and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) )
    {
       #Get the Old content from CEWPs
       $OldContent =  $webPart.Content
       $OldContentXml = $OldContent.InnerText
 
       #Replace the Old Links
       $XmlDoc = New-Object System.Xml.XmlDocument
       $NewContentXml= $XmlDoc.CreateElement("content") 
       $NewContentXml.InnerText= $OldContentXml.Replace($OldLink, $NewLink)
  
       #Set content and Save
       $webpart.Content = $NewContentXml    
 
       $webPartManager.SaveChanges($webPart);
       Write-Host "Replaced a link in $($Page.ServerRelativeUrl))"
     }
  }
#Check in and Publish the page  
$page.CheckIn("CEWP Updated")  
$Page.ListItem.File.Publish("CEWP Updated")}

Can we do it for Script Editor Web part? Sure, Simply replace “Microsoft.SharePoint.WebPartPages.ContentEditorWebPart” with “Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart”!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “Replace Content Editor Web Part (CEWP) Links with PowerShell

  • This script is really good. I was just wondering is there a way I can find $oldlinks on all pages and export to csv ?

    Reply
  • Hi,

    Getting below error for sharepoint publishing sites. Can you please help on this..
    Error:

    Exception calling “SaveChanges” with “1” argument(s): “The file is not checked
    out. You must first check out this document before making changes.”

    Reply
      • How to do it for SharePoint online?

        Reply
    • Its working in publishing sites without any error.Thanks a lot!!

      Reply

Leave a Reply

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