Tuesday, March 28, 2023

Powershell script to export all SEWP and CEWP content to CSv and text files

A working example to export all script editors and content editors content from an on-prem site collection. Handy for a SharePoint onprem to online migration project 


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 

#Configuration parameters

$webApp = Get-SPWebApplication "http://yourwebappurl"


$csvFile = "<path to export folder>\pageswitheditors.csv"


$SiteURL = "http://yoursitecollurl"

$ReportOutput="<path to export folder>\pageswitheditors.csv"

 

$ResultCollection = @()

 

#Get All Subsites in a site collection and iterate through each

$Site = Get-SPSite $SiteURL

$counter=0;

ForEach($Web in $Site.AllWebs)

{

    write-host Processing $Web.URL

    # If the Current Web is Publishing Web

    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))

    {

        #Get the Publishing Web

        $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)

                   

        #Get the Pages Library

        $PagesLib = $PubWeb.PagesList

     }

     else

     {

        $PagesLib = $Web.Lists["Site Pages"]

     }            

        #Iterate through all Pages 

        foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"})

        {

            $PageURL=$web.site.Url+"/"+$Page.File.URL

            $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

                  #Get All Web Parts data

            foreach ($WebPart in $WebPartManager.WebParts)

            {

             if ($webPart.GetType().Name -eq "ContentEditorWebPart" -or $webPart.GetType().Name -eq "ScriptEditorWebPart")

             {

             $counter+=1

             $content = $WebPart.Content.InnerText -replace "`n", " "

                $Result = New-Object PSObject

                $Result | Add-Member -type NoteProperty -name "Site URL" -value $web.Url

                $Result | Add-Member -type NoteProperty -name "Page URL" -value $PageURL

                $Result | Add-Member -type NoteProperty -name "Web Part Title" -value $WebPart.Title

                $Result | Add-Member -type NoteProperty -name "Web Part Type" -value $WebPart.GetType().ToString() 

                 $Result | Add-Member -type NoteProperty -name "Web Part Content" -value "content$counter.txt" 

 

                $ResultCollection += $Result

               

                 $obj = New-Object -TypeName PSObject -Property @{

    Content = $content

}

$obj |Export-csv   "<path to export folder>\content$counter.txt" -notypeinformation -Encoding UTF8


         }}}

}

#Export results to CSV

$ResultCollection | Export-csv $ReportOutput -notypeinformation  -Encoding UTF8


Powershell Script to export all active workflows in a web application to a CSV file

Here is a working example of exporting a list of all active workflows in a web application 


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


$webApp = Get-SPWebApplication "http://your webapp url"


$csvFile = "<path to export folder>\workflows-active.csv" 


$results = @()


foreach ($site in $webApp.Sites) {

    foreach ($web in $site.AllWebs) {

     if ($web.WorkflowAssociations.Count -gt 0) {

            $webObj = New-Object PSObject -Property @{

                "Title" = $web.Title

                "Url" = $web.Url

                "Workflows" = $web.WorkflowAssociations| where {$_.Enabled -eq $true} | Select-Object -ExpandProperty Name

            }

            $results += $webObj

        }

        # Get all the lists in the web

        $lists = $web.Lists

        foreach ($list in $lists) {

            # Get all the workflow associations for the list

            $workflowAssociations = $list.WorkflowAssociations | where {$_.Enabled -eq $true}

            foreach ($workflowAssociation in $workflowAssociations) {

                # Create an object to store the workflow details

                $workflowObj = New-Object PSObject -Property @{

                    "SiteCollection" = $web.Site.Url

                    "Web" = $web.Title

                    "List" = $list.Title

                    "ListURL" = $web.Url + "/" + $list.RootFolder.Url

                    "WorkflowName" = $workflowAssociation.Name

                    "IsActive" = $workflowAssociation.Enabled

                }

                # Add the workflow object to the results array

                $results += $workflowObj

            }

        }

        $web.Dispose()

    }

    $site.Dispose()

}


$results | Export-Csv -Path $csvFile -NoTypeInformation