|
4 | 4 |
|
5 | 5 | ## Summary |
6 | 6 |
|
7 | | -This sample will export the required site pages information to CSV. |
| 7 | +This PowerShell script uses the PnP PowerShell module to connect to a SharePoint Online site and extract metadata from the Site Pages library. It collects key information for each page—such as titles, URLs, authorship, creation and modification dates, layout type, and banner image details—and exports the results to a timestamped CSV file for reporting and analysis purposes. |
| 8 | + |
| 9 | +## Why It Matters / Real-World Scenarios |
| 10 | + |
| 11 | +Organizations often lack visibility into the volume, ownership, and status of modern SharePoint pages. This script enables administrators and site owners to: |
| 12 | +- Perform content audits prior to migrations, restructures, or clean-up initiatives |
| 13 | +- Identify outdated, unused, or orphaned pages |
| 14 | +- Support governance and compliance reviews by providing page ownership and modification history |
| 15 | +- Create an inventory of pages for site redesigns or information architecture planning |
| 16 | + |
| 17 | +## Benefits of the Reported Data |
| 18 | + |
| 19 | +- Improved Governance: Clear visibility into who created and last modified each page |
| 20 | +- Operational Insight: Understand page usage patterns and content lifecycle |
| 21 | +- Risk Reduction: Identify pages with missing ownership or stale content |
| 22 | +- Decision Support: Enables data-driven decisions for site cleanup, archiving, or redesign |
| 23 | +- Audit Readiness: Produces a structured, exportable record of SharePoint page metadata |
8 | 24 |
|
9 | 25 | ## Implementation |
10 | 26 |
|
11 | 27 | - Open Windows PowerShell ISE |
12 | 28 | - Create a new file |
13 | 29 | - Write a script as below, |
14 | 30 | - First, we will connect to the site from which site we want to get Site Pages library details. |
15 | | - - Then we will get Site Pages details and export it to CSV. |
| 31 | +- Then we will get Site Pages details and export it to CSV. |
| 32 | + |
| 33 | + |
16 | 34 |
|
17 | 35 | # [PnP PowerShell](#tab/pnpps) |
18 | 36 | ```powershell |
19 | | -$siteURL = "https://domain.sharepoint.com/" |
20 | | -$username = "username@domain.onmicrosoft.com" |
21 | | -$password = "********" |
22 | | -$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force |
23 | | -$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd |
24 | | -$dateTime = "_{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date) |
25 | | -$basePath = "E:\Contribution\PnP-Scripts\Logs\" |
26 | | -$csvPath = $basePath + "\SitePages" + $dateTime + ".csv" |
27 | | -$global:sitePagesCollection = @() |
28 | | -
|
29 | | -Function Login() { |
30 | | - [cmdletbinding()] |
31 | | - param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $creds) |
32 | | - Write-Host "Connecting to Site '$($siteURL)'" -f Yellow |
33 | | - Connect-PnPOnline -Url $siteURL -Credential $creds |
34 | | - Write-Host "Connection Successful" -f Green |
| 37 | +
|
| 38 | +# ============================ |
| 39 | +# Configuration |
| 40 | +# ============================ |
| 41 | +$SiteUrl = "https://domain.sharepoint.com/" |
| 42 | +$BasePath = "E:\Contribution\PnP-Scripts\Logs" |
| 43 | +$DateTime = Get-Date -Format "MM_dd_yy_HH_mm_ss" |
| 44 | +$CsvPath = Join-Path $BasePath "SitePages_$DateTime.csv" |
| 45 | +
|
| 46 | +# Ensure log directory exists |
| 47 | +if (-not (Test-Path $BasePath)) { |
| 48 | + New-Item -ItemType Directory -Path $BasePath | Out-Null |
| 49 | +} |
| 50 | +
|
| 51 | +# ============================ |
| 52 | +# Connect to SharePoint |
| 53 | +# ============================ |
| 54 | +function Connect-ToSharePoint { |
| 55 | + param ( |
| 56 | + [Parameter(Mandatory)] |
| 57 | + [string]$Url |
| 58 | + ) |
| 59 | +
|
| 60 | + try { |
| 61 | + Write-Host "Connecting to $Url..." -ForegroundColor Yellow |
| 62 | + Connect-PnPOnline -Url $Url -Interactive |
| 63 | + Write-Host "Connected successfully!" -ForegroundColor Green |
| 64 | + } |
| 65 | + catch { |
| 66 | + throw "Failed to connect to SharePoint: $($_.Exception.Message)" |
| 67 | + } |
35 | 68 | } |
36 | 69 |
|
37 | | -Function GetSitePagesDetails { |
| 70 | +# ============================ |
| 71 | +# Get Site Pages Metadata |
| 72 | +# ============================ |
| 73 | +function Get-SitePagesDetails { |
38 | 74 | try { |
39 | | - Write-Host "Getting site pages information..." -ForegroundColor Yellow |
40 | | - $sitePages = Get-PnPListItem -List "Site Pages" |
41 | | - ForEach ($Page in $sitePages) { |
42 | | - $sitePagesInfo = New-Object PSObject -Property ([Ordered] @{ |
43 | | - 'ID' = $Page.ID |
44 | | - 'Title' = $Page.FieldValues.Title |
45 | | - 'Description' = $Page.FieldValues.Description |
46 | | - 'Page Layout Type' = $Page.FieldValues.PageLayoutType |
47 | | - 'FileRef' = $Page.FieldValues.FileRef |
48 | | - 'FileLeafRef' = $Page.FieldValues.FileLeafRef |
49 | | - 'Created' = Get-Date -Date $Page.FieldValues.Created_x0020_Date -Format "dddd MM/dd/yyyy HH:mm" |
50 | | - 'Modified' = Get-Date -Date $Page.FieldValues.Last_x0020_Modified -Format "dddd MM/dd/yyyy HH:mm" |
51 | | - 'Modified By' = $Page.FieldValues.Modified_x0020_By |
52 | | - 'Created By' = $Page.FieldValues.Created_x0020_By |
53 | | - 'Author' = $Page.FieldValues.Author.Email |
54 | | - 'Editor' = $Page.FieldValues.Editor.Email |
55 | | - 'BannerImage Url' = $Page.FieldValues.BannerImageUrl.Url |
56 | | - 'File_x0020_Type' = $Page.FieldValues.File_x0020_Type |
57 | | - }) |
58 | | - $global:sitePagesCollection += $sitePagesInfo |
| 75 | + Write-Host "Retrieving Site Pages..." -ForegroundColor Yellow |
| 76 | +
|
| 77 | + $pages = Get-PnPListItem ` |
| 78 | + -List "Site Pages" ` |
| 79 | + -PageSize 500 ` |
| 80 | + -Fields ID,Title,Description,PageLayoutType,FileRef,FileLeafRef, |
| 81 | + Created_x0020_Date,Last_x0020_Modified, |
| 82 | + Modified_x0020_By,Created_x0020_By, |
| 83 | + Author,Editor,BannerImageUrl,File_x0020_Type |
| 84 | +
|
| 85 | + $results = foreach ($page in $pages) { |
| 86 | + [PSCustomObject]@{ |
| 87 | + ID = $page.Id |
| 88 | + Title = $page["Title"] |
| 89 | + Description = $page["Description"] |
| 90 | + PageLayoutType = $page["PageLayoutType"] |
| 91 | + FileRef = $page["FileRef"] |
| 92 | + FileName = $page["FileLeafRef"] |
| 93 | + Created = $page["Created_x0020_Date"] |
| 94 | + Modified = $page["Last_x0020_Modified"] |
| 95 | + CreatedBy = $page["Author"]?.Email |
| 96 | + ModifiedBy = $page["Editor"]?.Email |
| 97 | + BannerImageUrl = $page["BannerImageUrl"]?.Url |
| 98 | + FileType = $page["File_x0020_Type"] |
| 99 | + } |
59 | 100 | } |
| 101 | +
|
| 102 | + return $results |
60 | 103 | } |
61 | 104 | catch { |
62 | | - Write-Host "Error in getting site pages:" $_.Exception.Message -ForegroundColor Red |
| 105 | + throw "Error retrieving Site Pages: $($_.Exception.Message)" |
63 | 106 | } |
64 | | - Write-Host "Exporting to CSV..." -ForegroundColor Yellow |
65 | | - $global:SitePagesCollection | Export-Csv $csvPath -NoTypeInformation -Append |
66 | | - Write-Host "Exported to CSV successfully!" -ForegroundColor Green |
| 107 | +} |
| 108 | +
|
| 109 | +# ============================ |
| 110 | +# Export to CSV |
| 111 | +# ============================ |
| 112 | +function Export-ToCsv { |
| 113 | + param ( |
| 114 | + [Parameter(Mandatory)] |
| 115 | + [object[]]$Data, |
67 | 116 |
|
| 117 | + [Parameter(Mandatory)] |
| 118 | + [string]$Path |
| 119 | + ) |
| 120 | +
|
| 121 | + try { |
| 122 | + Write-Host "Exporting data to CSV..." -ForegroundColor Yellow |
| 123 | + $Data | Export-Csv -Path $Path -NoTypeInformation |
| 124 | + Write-Host "Export completed: $Path" -ForegroundColor Green |
| 125 | + } |
| 126 | + catch { |
| 127 | + throw "CSV export failed: $($_.Exception.Message)" |
| 128 | + } |
68 | 129 | } |
69 | 130 |
|
70 | | -Function StartProcessing { |
71 | | - Login($creds); |
72 | | - GetSitePagesDetails |
| 131 | +# ============================ |
| 132 | +# Main Execution |
| 133 | +# ============================ |
| 134 | +try { |
| 135 | + Connect-ToSharePoint -Url $SiteUrl |
| 136 | + $SitePages = Get-SitePagesDetails |
| 137 | + Export-ToCsv -Data $SitePages -Path $CsvPath |
73 | 138 | } |
| 139 | +catch { |
| 140 | + Write-Host $_ -ForegroundColor Red |
| 141 | +} |
| 142 | +
|
74 | 143 |
|
75 | | -StartProcessing |
76 | 144 | ``` |
77 | 145 | [!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] |
78 | 146 | *** |
79 | 147 |
|
| 148 | +## Output |
| 149 | + |
| 150 | +- **Format**: CSV |
| 151 | +- **Location**: Configurable output directory |
| 152 | +- **File Naming**: Timestamped (prevents overwriting previous reports) |
| 153 | + |
| 154 | +## Notes |
| 155 | +- Requires appropriate permissions to read the Site Pages library |
| 156 | +- Supports modern authentication (MFA-compatible) |
| 157 | +- Designed for single-site execution but easily extendable to multiple sites |
80 | 158 |
|
81 | 159 | ## Contributors |
82 | 160 |
|
83 | 161 | | Author(s) | |
84 | 162 | |-----------| |
85 | | -| Chandani Prajapati (https://github.com/chandaniprajapati) | |
| 163 | +| [Chandani Prajapati](https://github.com/chandaniprajapati) | |
| 164 | +| [Josiah Opiyo](https://github.com/ojopiyo) | |
| 165 | + |
| 166 | +*Built with a focus on automation, governance, least privilege, and clean Microsoft 365 tenants—helping M365 admins gain visibility and reduce operational risk.* |
| 167 | + |
| 168 | + |
86 | 169 |
|
87 | 170 | [!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] |
88 | 171 | <img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-export-all-site-pages-details" aria-hidden="true" /> |
0 commit comments