Introduction
Managing SharePoint environments often involves exploring and auditing content, especially pages and subsites across multiple site collections and web applications. Navigating this manually through the SharePoint UI can be tedious. Luckily, PowerShell offers a streamlined, automated way to achieve this.
Why Use PowerShell for SharePoint?
PowerShell is a powerful tool for SharePoint administrators, enabling tasks like:
- Bulk content discovery and management.
- Automation of repetitive processes.
- Access to detailed information not easily available through the SharePoint UI.
The Script: Exploring Pages and Subsites
This script is designed to iterate through all web applications, site collections, and subsites in your SharePoint environment, fetching details about the pages stored in each site.
1. Add the SharePoint Snap-In
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
This ensures the SharePoint PowerShell module is loaded. The -ErrorAction SilentlyContinue
flag suppresses errors if the snap-in is already added.
2. Define the Get-Pages
Function
function Get-Pages {
param(
[Microsoft.SharePoint.SPWeb]$Web
)
Write-Host " Pages in site: $($Web.Url)"
$pagesLibrary = $Web.Lists.TryGetList("Site Pages")
if ($pagesLibrary -ne $null) {
foreach ($page in $pagesLibrary.Items) {
Write-Host " Page: $($page["FileLeafRef"])"
}
} else {
Write-Host " No pages library found."
}
}
This function
- Accepts an
SPWeb
object (representing a SharePoint site) as a parameter.
- Checks if the "Site Pages" library exists.
- Iterates through all items (pages) in the library and displays their names.
3. Define the Get-Subsites
Function
function Get-Subsites {
param(
[Microsoft.SharePoint.SPWeb]$Web
)
foreach ($SubWeb in $Web.Webs) {
Write-Host " Subsite: $($SubWeb.Url)"
Get-Pages -Web $SubWeb
Get-Subsites -Web $SubWeb
$SubWeb.Dispose()
}
}
This recursive function:
- Accepts an
SPWeb
object.
- Iterates through all subsites of the provided site.
- For each subsite, calls
Get-Pages
to list its pages and recursively explores deeper subsites.
- Disposes of the
SPWeb
object after use to release memory.
4. Iterate Through Web Applications and Site Collections
$webApplications = Get-SPWebApplication
foreach ($webApp in $webApplications) {
Write-Host "Web Application: $($webApp.Url)"
foreach ($site in $webApp.Sites) {
Write-Host " Site Collection: $($site.Url)"
$rootWeb = $site.RootWeb
Write-Host " Root Site: $($rootWeb.Url)"
Get-Pages -Web $rootWeb
Get-Subsites -Web $rootWeb
$rootWeb.Dispose()
}
}
This section:
- Retrieves all web applications using
Get-SPWebApplication
.
- For each web application, iterates through its site collections using
$webApp.Sites
.
- Lists pages in the root site using
Get-Pages
and explores subsites using Get-Subsites
.
- Properly disposes of
SPWeb
objects to avoid memory leaks.
Example Output
Here’s what the output might look like:
Web Application: http://sharepointwebapp
Site Collection: http://sharepointwebapp/sites/hr
Root Site: http://sharepointwebapp/sites/hr
Pages in site: http://sharepointwebapp/sites/hr
Page: Welcome.aspx
Subsite: http://sharepointwebapp/sites/hr/benefits
Pages in site: http://sharepointwebapp/sites/hr/benefits
Page: BenefitsOverview.aspx
Expected Output
![output]()
Benefits of This Script
- Comprehensive Content Audit: Quickly gather an inventory of pages across your SharePoint environment.
- Time-Saving Automation: No need to manually navigate through the UI to find pages.
- Memory Management: Proper disposal of
SPWeb
objects ensures efficient use of resources.
- Customizable: Easily extend the script to fetch additional information, such as page metadata or permissions.
Conclusion
PowerShell is an indispensable tool for SharePoint administrators. This script demonstrates how to efficiently discover pages and subsites, empowering you to manage your SharePoint environment more effectively.
Try it out in your SharePoint setup and streamline your content audits today!