PnP PowerShell – How to get all site pages and metadata from given site and export to CSV file

Hi All,
Greetings for the day!!!
Recently got a chance to write a small PowerShell script so thought to share.
PowerShell script : Fetching all “Site Pages” and respective metadata from the specified Site.
Prerequisites
- PnP online PowerShell module is installed
- If PnP online PowerShell module is not installed then please download latest PowerShell module and install
- We can install PowerShell PnP module using Install-Module CMDLET
Install-Module PnP.PowerShell
Use Case
- To improve findability to our content, we are planning to tag all our pages.
- We are planning to tag “Enterprise Keyword” field.
- Before starting tagging, we need the report of already tagged paged and not tagged pages. So that we can plan the tags appropriately.
- We wrote the simple PowerShell script to read “Site Pages” library and get the metadata and write to CSV file.
- We are using PnP PowerShell.
Detailed script
STEP 1 – Import the respective Module
- We are using “PnP PowerShell” module.
- First step – we will check if “PnP PowerShell” module is imported or not.
- We will use “Get-Module” PowerShell CMDLET.
#Verify if "PnP.PowerShell" module is imported or not.
#If now then we will import the module.
if ( -not (Get-Module -ListAvailable -Name PnP.PowerShell)) {
Write-Host "Module not imported exists"
Import-Module PnP.PowerShell
} #if ( -not (Get-Module -ListAvailable -Name PnP.PowerShell))
STEP 2 – Variable Declaration
- Declared the required variables
#region Knowledge Junction tenant Variable declarations
#Client ID - to connect to the tenant
$clientId = "768437d9-957a-4a5e-b91a-f68213bf6d33"
#CSV File location - Path where we will generate our CSV file
$CSVFile = "C:\sitepages.csv"
#Site URL - Site from which we need to fetch the Site Pages and meta data
$SiteURL = "https://knowledgejunction1.sharepoint.com/sites/DemoStorageSite_archive"
#endregion
STEP 3 – Connect to the given Site and get all the site pages
- Using Connect-PnPOnline we will connect to given site
Connect-PnPOnline -Url $SiteURL -Interactive -ClientId $clientId
- Once we successfully connected to the Site, using “Get-PnPListItem” we will get all the Site Pages
#Get All Site Pages
$SitePages = Get-PnPListItem -List "SitePages"
STEP 4 – Iterate through each Site Page and collect the metadata
#array declaration
$PagesDataColl = @()
#Collect Site Pages data - Title, URL and other properties
ForEach($Page in $SitePages){
$Data = New-Object PSObject -Property ([Ordered] @{
PageName = $Page.FieldValues.Title
RelativeURL = $Page.FieldValues.FileRef
ModifiedBy = $Page.FieldValues.Editor.Email
ID = $Page.ID
EnterpriseKeywords = $page.FieldValues.TaxKeyword.Count
})
$PagesDataColl += $Data
}#ForEach($Page in $SitePages){
STEP 5 – Export the collected data to CSV file
#Export data to CSV File
$PagesDataColl | Export-Csv -Path $CSVFile -NoTypeInformation
COMPLETE SCRIPT
#region Module Check
#Verify if "PnP.PowerShell" module is imported or not.
#If now then we will import the module.
if ( -not (Get-Module -ListAvailable -Name PnP.PowerShell)) {
Write-Host "Module not imported exists"
Import-Module PnP.PowerShell
} #if ( -not (Get-Module -ListAvailable -Name PnP.PowerShell))
#endregion
#region Knowledge Junction tenant Variable declarations
#Client ID - to connect to the tenant
$clientId = "768437d9-957a-4a5e-b91a-f68213bf6d44"
#CSV File location - Path where we will generate our CSV file
$CSVFile = "C:\sitepages.csv"
#Site URL - Site from which we need to fetch the Site Pages and meta data
$SiteURL = "https://knowledgejunction1.sharepoint.com/sites/DemoStorageSite_archive"
#endregion
#Connect to PnP
Connect-PnPOnline -Url $SiteURL -Interactive -ClientId $clientId
#Get All Site Pages
$SitePages = Get-PnPListItem -List "SitePages"
$PagesDataColl = @()
#Collect Site Pages data - Title, URL and other properties
ForEach($Page in $SitePages)
{
$Data = New-Object PSObject -Property ([Ordered] @{
PageName = $Page.FieldValues.Title
RelativeURL = $Page.FieldValues.FileRef
CreatedBy = $Page.FieldValues.Created_x0020_By
ModifiedBy = $Page.FieldValues.Editor.Email
ID = $Page.ID
EnterpriseKeywords = $page.FieldValues.TaxKeyword.Count
})
$PagesDataColl += $Data
}#ForEach($Page in $SitePages){
#Export data to CSV File
$PagesDataColl | Export-Csv -Path $CSVFile -NoTypeInformation
REFERENCES
- 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🙂 Life is Beautiful🙂
Have a nice day🙂🙂
