Get SharePoint Page Layouts Usage Analysis Report using PowerShell
Requirement: Get an inventory of all available Page layouts and their usage in the SharePoint site Collection.
How to Get All Page Layouts in SharePoint?
SharePoint provides a page layout feature that allows site administrators to create and manage reusable page templates. This can be helpful when you need to create a new page quickly on your site or when you want to maintain a consistent look between pages. However, it can be difficult to determine which pages are using which layouts, especially if your site has a lot of pages. This blog post will show you how to use PowerShell to generate a report that lists all the pages on your site and their corresponding layout usage.
If you get into the Site Settings page, You can find page layouts under “Page layouts and site templates” or “Master pages and page layouts” links.
Get All Available Page Layouts of a Publishing Site Collection
SharePoint Page layouts are scoped at the site collection level. Let’s get all page layouts from a site:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Get-PageLayouts
{
param
(
[Parameter(Mandatory=$true)][Microsoft.SharePoint.SPSite]$Site
)
#Get the Publishing Site object
[Microsoft.Sharepoint.Publishing.PublishingSite]$PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
#Get All page layouts
$PageLayouts = $PublishingSite.GetPageLayouts($false)
ForEach($PageLayout in $PageLayouts)
{
Write-host $PageLayout.Name : $PageLayout.ServerRelativeUrl
}
}
#Get a Site collection
$Site = Get-SPSite "https://Opera.crescent.com/"
#Call the function to get all available page layouts
Get-PageLayouts -Site $Site
Get Page Layouts Usage in SharePoint Site Collection using PowerShell:
Now, lets audit page layouts usage in a particular site collection:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Get-PageLayoutsUsage
{
param
(
[Parameter(Mandatory=$true)][Microsoft.SharePoint.SPSite]$Site,
[Parameter(Mandatory=$true)][String]$ReportFile
)
#Array to store Result
$ResultSet = @()
#Iterate through each web of the site collection
ForEach($web in $Site.AllWebs)
{
Write-host -f Cyan "Scanning site $($Web.URL)..."
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
if($PublishingWeb.PagesList)
{
foreach($Page in $PublishingWeb.GetPublishingPages())
{
#Get the page layout details
$Result = new-object PSObject
$Result | add-member -membertype NoteProperty -name "URL" -Value $web.Url
$Result | add-member -membertype NoteProperty -name "Page URL" -Value $Page.Uri.ToString()
$Result | add-member -membertype NoteProperty -name "PageLayout URL" -Value $Page.Layout.ServerRelativeUrl
$Result | add-member -membertype NoteProperty -name "PageLayout Name" -Value $Page.Layout.Name
$ResultSet += $Result
}
}
}
#Export Result to csv file
$ResultSet | Export-Csv $ReportFile -notypeinformation
Write-Host "Page Layouts Usage Report Generated Successfully!" -f Green
}
#Get a Site collection
$Site = Get-SPSite "https://portal.crescent.com/"
$ReportFile="C:\Temp\PageLayouts.csv"
#Call the function to get page layouts usage
Get-PageLayoutsUsage -Site $Site -ReportFile $ReportFile
This script generates a CSV file in the below format: