In this article, I will provide information on how to get all the content editor webparts present under a web application using PowerShell.
Below are the components used in this document.
  1. PowerShell
  2. SharePoint 2016

Introduction

Some time ago we get a request to collect an inventory on the current SharePoint environment; including how many content editor Webparts and the number of lists and libraries under a web application. Below is the step by step way to get all the content editor webparts present under a web application using PowerShell.

Power Shell File

Step 1
Create a text file. Name it as “GetTotalCEWP.ps1” file. Note that you will have to change the extension from “.txt” to “.ps1”
Step 2
Insert the below code to get the web application URL as an input from the user.
  1. [CmdletBinding()]
  2. param( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Input the Web Application URL.")] [string]$WebApp)
Step 3
Insert the below code to load the SharePoint PowerShell snap in.
  1. if ((Get - PSSnapin - Name Microsoft.SharePoint.PowerShell - ErrorAction SilentlyContinue) - eq $null) {
  2. Write - Host "Loading SharePoint PowerShell"
  3. Add - PSSnapin Microsoft.SharePoint.PowerShell
  4. }
Step 4
Insert the below code to get all the publishing pages and then fetch the content editor webparts from those pages.
  1. $SPWebApp = Get - SPWebApplication $WebApp - EA SilentlyContinue
  2. if ($SPWebApp - eq $null) {
  3. Write - Error "$WebApp url is not a valid!"
  4. } else {
  5. $allsites = $SPWebApp.Sites
  6. foreach($site in $allsites) {
  7. try {
  8. $allwebs = $site.AllWebs
  9. foreach($web in $allwebs) {
  10. try {
  11. if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
  12. $pubpages = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
  13. $allpages = $pubpages.GetPublishingPages()
  14. foreach($page in $allpages) {
  15. GetCEWPFunction - url $page.Url
  16. }
  17. }
  18. } catch {} finally {
  19. $web.Dispose()
  20. }
  21. }
  22. } catch {} finally {
  23. $site.Dispose()
  24. }
  25. }
  26. }
Step 5
There might be scenarios where we have added the content Editor webparts inside the List/Libraries forms (edit/view/display). Insert the below code to retrieve the same. We have to insert this code just before the first try/catch ends.
  1. GetCEWPFunction - url $page.Url
  2. }
  3. }
  4. $lists = $web.GetListsOfType("DocumentLibrary") | ? {
  5. $_.IsCatalog - eq $false
  6. }
  7. foreach($list in $lists) {
  8. $allviews = $list.Views
  9. foreach($view in $allviews) {
  10. GetCEWPFunction - url $view.Url
  11. }
  12. $allforms = $list.Forms
  13. foreach($form in $allforms) {
  14. GetCEWPFunction - url $form.Url
  15. }
  16. }
  17. }
  18. catch {} finally {
  19. $web.Dispose()
  20. }
  21. }
  22. } catch {} finally {
  23. $site.Dispose()
  24. }
  25. }
  26. }
Step 6
Insert the below code to have the function named “GetCEWPFunction “ which would actually count the content editor webpart.
  1. function GetCEWPFunction([string] $url) {
  2. $manager = $web.GetLimitedWebPartManager($url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  3. $allwebParts = $manager.WebParts
  4. if ($allwebParts.Count - ne 0) {
  5. foreach($webPart in $allwebParts) {
  6. if ($webPart.GetType() - eq[Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) {
  7. if ($webPart.ContentLink.Length - gt 0) {
  8. $file = $web.GetFile($webPart.ContentLink)
  9. $data = $file.OpenBinary()
  10. $encode = New - Object System.Text.ASCIIEncoding
  11. $contents = $encode.GetString($data)
  12. if ($contents.ToLower().Contains("<script>")) {
  13. Write - Output "$($web.Url)/$url (CONTENTLINK)"
  14. }
  15. break
  16. }
  17. if ($webPart.Content.InnerText.Contains("<script>")) {
  18. Write - Output "$($web.Url)/$url (HTML)"
  19. }
  20. }
  21. }
  22. }
  23. }
Step 7
The full file will look as attached with this article.
Step 8
Save your PowerShell File and test it by running below command. It will list down all the content editor webparts present under a web application
.\GetTotalCEWP.ps1 -WebApp http://portal.intranet.com
That is it. I hope you have learned something new from this article and will utilize this in your work.