Move SharePoint Pages
This PnP PowerShell script collection provides two methods for moving SharePoint pages between folders or sites. Moving pages is essential for content reorganization, site restructuring, and maintaining organized navigation hierarchies.
Purpose
Page movement functionality helps with:
- Reorganizing site content structure
- Moving pages between different folders
- Migrating content during site restructuring
- Maintaining proper navigation hierarchies
- Batch operations for multiple page moves
Prerequisites
- PnP PowerShell module installed
- Site collection administrator permissions
- Connection to your SharePoint Online site
- Knowledge of source and destination folder paths
Script 1: Move Single Page
# Configuration
$SiteUrl = "https://tenantName.sharepoint.com/sites/siteName"
$SourceFolder = "SitePages/SourceFolder/"
$TargetFolder = "SitePages/TargetFolder"
$ClientId = ""
# Connect to SharePoint
Connect-PnPOnline -Url $SiteUrl -Interactive -ClientId $ClientId
# Get all pages from source folder
$Pages = Get-PnPFolderItem -FolderSiteRelativeUrl $SourceFolder -ItemType File | Where-Object {$_.Name -like "*.aspx"}
Write-Host "Found $($Pages.Count) pages to move" -ForegroundColor Yellow
Write-Host ""
# Loop through each page and move it
foreach ($Page in $Pages) {
Write-Host "Moving: $($Page.Name)" -ForegroundColor Cyan
try {
Move-PnPFile -SourceUrl "$SourceFolder/$($Page.Name)" -TargetUrl "$TargetFolder/$($Page.Name)" -Force -OverwriteIfAlreadyExists
Write-Host " ✓ Moved successfully" -ForegroundColor Green
}
catch {
Write-Host " ✗ Error: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host ""
}
Write-Host "Move complete!" -ForegroundColor Yellow
Script 2: Move Multiple Pages (Batch Operation)
# Configuration
$ClientId = ""
$SiteUrl = "https://tenantName.sharepoint.com/sites/siteName"
$SourceFolder = "SitePages/SourceFolder"
$TargetFolder = "SitePages/TargetFolder"
$ErrorLogPath = ""
# Array of files to move
$FilesToMove = @(
"fileName.aspx",
"fileName1.aspx",
"fileName2.aspx"
)
# Initialize error log array
$ErrorLog = @()
# Connect to SharePoint
try {
Connect-PnPOnline -Url $SiteUrl -Interactive -ClientId $ClientId
Write-Host "✓ Connected to SharePoint" -ForegroundColor Green
Write-Host ""
}
catch {
Write-Host "✗ Failed to connect to SharePoint: $($_.Exception.Message)" -ForegroundColor Red
exit
}
Write-Host "Moving $($FilesToMove.Count) pages..." -ForegroundColor Yellow
Write-Host ""
# Loop through each file and move it
foreach ($FileName in $FilesToMove) {
Write-Host "Moving: $FileName" -ForegroundColor Cyan
try {
Move-PnPFile -SourceUrl "$SourceFolder/$FileName" -TargetUrl "$TargetFolder/$FileName" -Force -OverwriteIfAlreadyExists
Write-Host " ✓ Moved successfully" -ForegroundColor Green
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Host " ✗ Error: $ErrorMessage" -ForegroundColor Red
# Log error to array
$ErrorLog += [PSCustomObject]@{
FileName = $FileName
SourceFolder = $SourceFolder
TargetFolder = $TargetFolder
ErrorMessage = $ErrorMessage
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
Write-Host ""
}
# Export errors to CSV if any occurred
if ($ErrorLog.Count -gt 0) {
$ErrorLog | Export-Csv -Path $ErrorLogPath -NoTypeInformation -Encoding UTF8
Write-Host "✗ $($ErrorLog.Count) errors occurred. Log saved to: $ErrorLogPath" -ForegroundColor Red
}
else {
Write-Host "✓ All pages moved successfully - no errors!" -ForegroundColor Green
}
Write-Host ""
Write-Host "Move complete!" -ForegroundColor Yellow
Usage Notes
- Update the site URL to match your SharePoint environment
- Ensure destination folders exist before running the script
- Test with a single page first before batch operations
- Consider updating navigation links if moved pages are in site navigation
- Check page permissions after moving to ensure proper access
- For cross-site moves, use Copy-PnPFile followed by Remove-PnPFile