Add Content Editor Web Part On SharePoint 2013 Sites Using PowerShell

Today, I would like to share my knowledge on the recently concluded SharePoint migration project in our company.

After the migration of sites, we were asked by our client to put a banner on the old sites so when the members access the site, they will see the banner and upon clicking the banner, they would be redirected to the newly migrated site.

The real problem with this task was that we had only two members in the team and the total number of sites migrated was around 65. So, we had to literally go to all the sites and put the banner as Content Editor web part on the homepage of each site.

We thought of writing a PowerShell script which will add banner as Content Editor web part on all the old sites. Here are steps involved in the process.

  • Prepare the CSV file which contains two columns, namely “Site URL,” which will have all the old site urls for which we need to add banner, and “New Site” which will have all the new site URLs which is the redirection URL on the corresponding old site.

  • Execute the script (which was done by SharePoint admin)

  • After successful execution of the script, verify whether the banner has been added or not, and check the redirection URL.
    1. Add - PSSnapin microsoft.sharepoint.powershell  
    2. #function to set the content editor webpart  
    4. function SetContentEditorWebPart($path)   
    5. {  
    6.   #get contents from csv file  
    7.     $csvcontents = Import - Csv - Path $path  
    8.     foreach($site in $csvContents)   
    9.     { #iterate through each site and add content editor webpart on the home page of the site  
    10.         $url = $site.  
    11.         'Site Url'  
    12.         $website = Get - SPSite - Identity $url  
    13.         $web = $website.RootWeb  
    14.         $wpmgr = $web.GetLimitedWebPartManager($web.RootFolder.WelcomePage, [system.web.ui.webcontrols.webparts.personalizationscope]::Shared)  
    15.         $contenteditorwebpart = New - Object microsoft.sharepoint.webpartpages.ContentEditorWebPart  
    16.         $contenteditorwebpart.Title = "Site Migration Information"  
    17.         $contenteditorwebpart.ChromeType = [system.web.ui.webcontrols.webparts.PartChromeType]::TitleAndBorder[system.Xml.XmlDocument] $content = New - Object system.Xml.XmlDocument[system.Xml.XmlElement] $contentxml = $content.CreateElement("MigrationContent")  
    18.         $contentxml.InnerText = "Your SharePoint site has migrated to O365!" + " " + "<a href=" + $site.  
    19.         'New Site' + ">Here</a>" + " " + "is the link for the O365 site.Please use email address to log in but no password should be needed."  
    20.         $contenteditorwebpart.Content = $contentxml  
    21.         $wpmgr.AddWebPart($contenteditorwebpart, "Header", 1)  
    22.         $wpmgr.SaveChanges($contenteditorwebpart)  
    23.     }  
    25. }#Provide the path of Csv File containing site urls and call the  
    26. function  
    27. SetContentEditorWebPart("Path for the CSV File")  

The above is the PowerShell script which was used to complete the required task. Let me brief you on the functionality of the script.

  • SetContentEditorWebPart is a function which will have the path of the CSV file as mandatory parameter.
  • We are getting all the content from the CSV file.
  • We are looping through the old sites and for each site, we are getting the root web and obtaining the web part manager class. For web part manager class, we need to pass the welcome page of the site, and scope of the web part manager object that applies to a property on web parts control.
  • Create a new instance of the Content Editor web part class.
  • Set the Chrome type and Title for the Content Editor web part object
  • Create an XML document instance. Here, XML element is used as the content so update with the appropriate content.
  • Use the "Add Web Part" method of the web part manager instance by passing Content Editor web part object, Web Part zone, and the zone index.
  • Finally, save the changes to the web part manager.

Now, you should be able to see the Content Editor web part on the sites.


This PowerShell Script will work only on the homepage of the sites which are of web part type only. It won’t work on the sites which have Wiki pages as homepage.

Ebook Download
View all
View all