PowerShell scripts to extract portal content

Loading

Extracting Power Pages (formerly Power Apps Portals) content via PowerShell is typically done using a combination of Power Platform CLI (PAC CLI) and PowerShell scripting to automate the process. This is essential for backups, source control, or CI/CD pipelines.

Below is a complete PowerShell-based guide to extract portal content using pac paportal commands in scripts.


Prerequisites

Before running PowerShell scripts:

  1. Install Power Platform CLI powershellCopyEditdotnet tool install --global Microsoft.PowerApps.CLI.Tool
  2. Ensure PowerShell 5.1+ or PowerShell Core (7.x) is installed
  3. Have App Registration details:
    • Application (client) ID
    • Client Secret
    • Tenant ID
    • Environment URL (e.g., https://org.crm.dynamics.com)

Step-by-Step PowerShell Script


Script: Extract Power Pages Portal Content

# Define variables
$environmentUrl = "https://yourorg.crm.dynamics.com"
$clientId = "your-client-id"
$clientSecret = "your-client-secret"
$tenantId = "your-tenant-id"
$portalPath = "C:\PortalExports\MyPortal"
$solutionName = "MyPortalSolution"

# Create output folder if it doesn't exist
if (!(Test-Path -Path $portalPath)) {
New-Item -ItemType Directory -Path $portalPath
}

# Authenticate with PAC CLI
Write-Host "Authenticating to Power Platform environment..."
pac auth create --url $environmentUrl --applicationId $clientId --clientSecret $clientSecret --tenant $tenantId

# Select the authentication profile (usually index 0 if just created)
pac auth select --index 0

# List portals in the environment (optional step)
Write-Host "Available portals in the environment:"
pac paportal list

# Download portal content
Write-Host "Exporting portal content to $portalPath..."
pac paportal download --path "$portalPath"

Write-Host "Portal export completed successfully."

Automating with Parameters (Reusable Script)

Save this as Export-PowerPages.ps1:

param (
[string]$EnvironmentUrl,
[string]$ClientId,
[string]$ClientSecret,
[string]$TenantId,
[string]$ExportPath
)

if (!(Test-Path -Path $ExportPath)) {
New-Item -ItemType Directory -Path $ExportPath
}

pac auth create --url $EnvironmentUrl --applicationId $ClientId --clientSecret $ClientSecret --tenant $TenantId
pac auth select --index 0
pac paportal download --path $ExportPath

Write-Host "Export completed to $ExportPath"

Usage:

.\Export-PowerPages.ps1 `
-EnvironmentUrl "https://yourorg.crm.dynamics.com" `
-ClientId "your-client-id" `
-ClientSecret "your-client-secret" `
-TenantId "your-tenant-id" `
-ExportPath "C:\PortalBackup"

Validating Extracted Content

After export, the directory will contain folders like:

  • Web Pages/
  • Web Files/
  • Web Templates/
  • Entity Forms/
  • Site Settings/
  • Content Snippets/
  • page-not-found.json, sitemap.json, etc.

This structure can be version-controlled using Git or zipped for backups.


Optional: Export Associated Solution

If your portal is linked to a solution, you can export it too:

pac solution export --name "MyPortalSolution" `
--path "C:\PortalExports\MyPortalSolution.zip" `
--managed false

Scheduling via Task Scheduler or Azure DevOps

  • Wrap the script in a scheduled task to run backups daily
  • Integrate into Azure DevOps or GitHub Actions pipeline for source control

Best Practices

  • Store secrets (client secret) in secure vaults like Azure Key Vault or Windows Credential Store
  • Regularly update the backup directory with version control
  • Always validate portal export by checking file timestamps and key JSONs

Leave a Reply

Your email address will not be published. Required fields are marked *