Collaborate, Innovate, Automate

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:

Prerequisites

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