PowerShell – Script to enlist all the pages of Site Collection and the metadata – Export to CSV file

Hi All,
Greetings for the day!!!
Today sharing PowerShell script which I recently wrote.
UseCase: Read all the pages of Site Collection and metadata
Background:
- In our tenant we have multiple modern site collections.
- Each Site Collection have multiple modern site pages.
- Suddenly while editing site pages our editor face challenge to publish the page.
- Editors are not capable of publishing the pages.
- We are getting the next error

Error : Could not publish your page
We’re sorry. We encountered an unexpected error. Please refresh the page and try again.
- Even we went to “Site Pages” library and from there try to edit properties.
- Yet, while saving the page getting an below error
Error: “The given guid does not exist in the term store”
Cause:
- After bit googling and with trial and error method we found that there is an issue with “EnterPrise keywords” on the pages
- GUID of “Enterprise Keyword” term is changed and which is causing an issue.
- For time being we applied workaround
- Edit the page
- Remove “Enterprise Keywords“
- Publish the Page
- Again edit the page
- Add “Enterprise Keywords“
- Publish the page again
- To resolve this issue or to know the impact across the tenant we need the list of pages where “Enterprise Keywords” are added
- We have written the PowerShell script to navigate to all our Site Collections from our tenant, read all the pages and metadata – “Enterprise Keywords” and export to CSV
PREREQUISITES
- List of all Sites from the tenant in CSV file. Below is the snap of Knowledge-Junction tenant sites

- PnP.PowerShell module – Make sure PnP.PowerShell module is installed

- Entra client id (Entra application) having permission to all SharePoint sites in tenant
- We have detailed articles on creating Entra Application / Application registration , permission management. Please refer REFERENCE section.
Detailed PowerShell script – Get all Site Pages and metadata from all Site Collections and export to CSV file
- We are using PnP PowerShell.
- Make sure that PnP PowerShell module is installed.
Import-Module PnP.PowerShell
- Declare the required variables
#CSV file path - CSV file having list of sites from whhich we need to read all the site pages and metadata
$CSVFilePath = "D:\Powershell\KJ Sites pages.csv"
#CSV file path - CSV file in which result will be exported
$CSVFile = "D:\Powershell\SitePagesList\KJ_SitePages.csv"
#client id - having read permissions to all SharePoint sites in tenant
$clientId = "c0bbe3ds-2w10-4r14-q13e-4wer6t23yu01"
#Declare empty array object to store the result
$PagesDataColl = @()
- Loop through all site collection from the site collections CSV file
#loop through all site collections from CSV file
Import-CSV $CSVFilePath | ForEach-Object {
#Full Urls - "URL" is the column in CSV file
$SiteURL = ($_."URL")
}
- Connect to the site
#connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive -ClientId $clientId
- Get all site pages from “Site Pages” library
#Get All Site Pages
$SitePages = Get-PnPListItem -List "Site Pages"
- Loop through all the pages and collect the properties/metadata
ForEach($Page in $SitePages)
{
#entreprise keywords - internal field name - TaxKeyword
$EnterpriseKeywords = $Page.FieldValues["TaxKeyword"]
$EnterpriseKeywordsLabels=""
foreach($EnterPriseKeyword in $EnterpriseKeywords)
{
$EnterpriseKeywordsLabels+= $EnterPriseKeyword.Label + ";"
}
$Data = New-Object PSObject -Property ([Ordered] @{
PageName = $Page.FieldValues.Title
RelativeURL = $Page.FieldValues.FileRef
ID = $Page.ID
Title = $Page.FieldValues.Title
_EnterpriseKeywords = $EnterpriseKeywordsLabels
})
$PagesDataColl += $Data
}#ForEach($Page in $SitePages)
- Export result into CSV file
- We have detailed article on exporting data to CSV file. PowerShell tutorial – Write a CSV file with PowerShell using the Export-CSV – https://knowledge-junction.in/2022/10/31/write-a-csv-file-with-powershell-using-the-export-csv-function/
$PagesDataColl+=""
#Export data to CSV File
$PagesDataColl | Export-Csv -Path $CSVFile -NoTypeInformation -Append
COMPLETE SCRIPT
#CSV file path - CSV file having list of sites from whhich we need to read all the site pages and metadata
$CSVFilePath = "D:\Powershell\KJ Sites pages.csv"
#CSV file path - CSV file in which result will be exported
$CSVFile = "D:\Powershell\SitePagesList\KJ_SitePages.csv"
#client id - having read permissions to all SharePoint sites in tenant
$clientId = "c0bbe3ds-2w10-4r14-q13e-4wer6t23yu01"
#Declare empty array object to store the result
$PagesDataColl = @()#CSV file path - CSV file having list of sites from whhich we need to read all the site pages and metadata
$CSVFilePath = "D:\Powershell\KJ Sites pages.csv"
#CSV file path - CSV file in which result will be exported
$CSVFile = "D:\Powershell\SitePagesList\KJ_SitePages.csv"
#client id - having read permissions to all SharePoint sites in tenant
$clientId = "c0bbe3ds-2w10-4r14-q13e-4wer6t23yu01"
#Declare empty array object to store the result
$PagesDataColl = @()
#loop through all site collections from CSV file
Import-CSV $CSVFilePath | ForEach-Object {
#Full Urls - "URL" is the column in CSV file
$SiteURL = ($_."URL")
#connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive -ClientId $clientId
#Get All Site Pages
$SitePages = Get-PnPListItem -List "Site Pages"
ForEach($Page in $SitePages)
{
#entreprise keywords - internal field name - TaxKeyword
$EnterpriseKeywords = $Page.FieldValues["TaxKeyword"]
$EnterpriseKeywordsLabels=""
foreach($EnterPriseKeyword in $EnterpriseKeywords)
{
$EnterpriseKeywordsLabels+= $EnterPriseKeyword.Label + ";"
}
$Data = New-Object PSObject -Property ([Ordered] @{
PageName = $Page.FieldValues.Title
RelativeURL = $Page.FieldValues.FileRef
ID = $Page.ID
Title = $Page.FieldValues.Title
_EnterpriseKeywords = $EnterpriseKeywordsLabels
})
$PagesDataColl += $Data
}#ForEach($Page in $SitePages)
$PagesDataColl+=""
#Export data to CSV File
$PagesDataColl | Export-Csv -Path $CSVFile -NoTypeInformation -Append
}#Import-CSV $CSVFilePath | ForEach-Object {
REFERENCES
- Small Tips and Tricks – Microsoft Entra admin center – How to navigate – Microsoft Entra admin center – https://knowledge-junction.in/2022/12/19/small-tips-and-tricks-microsoft-entra-admin-center-how-to-navigate-microsoft-entra-admin-center/
- Microsoft Entra – registering new application and assigning permissions to access Microsoft Graph APIs – https://knowledge-junction.in/2024/01/18/microsoft-entra-registering-new-application-and-assigning-permissions-to-access-microsoft-graph-apis/
- Exploring Microsoft Entra ID – Discussing What is Application registration? What is Application and Service Principal object – https://knowledge-junction.in/2024/03/27/exploring-ms-entra-application-registration/
- Microsoft Graph – How to create / register app in Microsoft Entra – https://knowledge-junction.in/2024/05/06/msgraph-create-app-microsoft-entra/
- PowerShell tutorial – Write a CSV file with PowerShell using the Export-CSV – https://knowledge-junction.in/2022/10/31/write-a-csv-file-with-powershell-using-the-export-csv-function/
Thank you for reading!
Have a nice day 🙂 LIFE IS BEAUTIFUL:)

You must be logged in to post a comment.