求可每周执行的SharePoint Online Term Store备份PowerShell脚本
I’ve got you covered with a pair of practical PowerShell scripts—one for automated weekly backups, another for straightforward restoration. These use PnP PowerShell (the industry standard for SPO automation) and work with JSON format to preserve your Term Store’s full structure, making recovery smooth when you need it.
Prerequisites
- First, install the latest PnP PowerShell module:
Install-Module -Name PnP.PowerShell -Force -AllowClobber - You’ll need Term Store Administrator or Global Administrator permissions to access and modify your tenant’s Term Store.
Weekly Backup Script
Save this as Backup-SPOTermStore.ps1—it connects to your tenant, exports the entire Term Store (groups, sets, terms, and properties) to a timestamped JSON file, so you can easily track backup versions.
# Configuration - Update these values to match your tenant $tenantUrl = "https://yourtenantname-admin.sharepoint.com" # Use your SPO admin URL $backupPath = "C:\SPOBackups\TermStore" # Pick a secure, accessible storage location $timestamp = Get-Date -Format "yyyyMMdd-HHmmss" $backupFile = Join-Path -Path $backupPath -ChildPath "SPOTermStore-Backup-$timestamp.json" # Create backup folder if it doesn't exist if (-not (Test-Path -Path $backupPath)) { New-Item -Path $backupPath -ItemType Directory -Force | Out-Null } try { # Connect to SharePoint Online Write-Host "Connecting to your SPO tenant..." -ForegroundColor Cyan # Use -DeviceLogin instead of -Interactive for non-interactive scheduled runs Connect-PnPOnline -Url $tenantUrl -Interactive # Grab the default Term Store (works for most tenants) $termStore = Get-PnPTermStore # Export full Term Store structure to JSON Write-Host "Exporting Term Store to $backupFile..." -ForegroundColor Cyan $termStore | Select-Object -ExpandProperty Groups | ConvertTo-Json -Depth 10 | Out-File -FilePath $backupFile -Encoding UTF8 Write-Host "Backup done! File saved to: $backupFile" -ForegroundColor Green } catch { Write-Host "Backup failed: $_" -ForegroundColor Red } finally { # Clean up the connection Disconnect-PnPOnline -ErrorAction SilentlyContinue }
Scheduling Weekly Runs
To automate this weekly:
- Open Task Scheduler on your Windows machine.
- Create a new task, set a trigger to run "Weekly" (pick your preferred day/time).
- For the action, select "Start a program" and use
powershell.exeas the program. - Add arguments:
-ExecutionPolicy Bypass -File "C:\Path\To\Backup-SPOTermStore.ps1" - Run the task under an account with the required SPO permissions. For non-interactive runs, swap
-Interactivewith-DeviceLoginin the script (you’ll authenticate once initially).
Restore Script
Use this to recover from a backup JSON file—save it as Restore-SPOTermStore.ps1:
# Configuration - Update these values $tenantUrl = "https://yourtenantname-admin.sharepoint.com" $backupFile = "C:\SPOBackups\TermStore\SPOTermStore-Backup-20240520-143000.json" # Replace with your backup path $overwriteExisting = $false # Set to $true only if you want to replace matching existing terms/groups try { # Connect to SharePoint Online Write-Host "Connecting to your SPO tenant..." -ForegroundColor Cyan Connect-PnPOnline -Url $tenantUrl -Interactive # Get the target Term Store $termStore = Get-PnPTermStore # Load backup data Write-Host "Reading backup file..." -ForegroundColor Cyan $backupData = Get-Content -Path $backupFile -Raw | ConvertFrom-Json # Restore each Term Group foreach ($group in $backupData) { # Check if group already exists $existingGroup = Get-PnPTermGroup -Identity $group.Name -ErrorAction SilentlyContinue if ($existingGroup -and $overwriteExisting) { Write-Host "Removing existing group: $($group.Name) (overwrite enabled)" -ForegroundColor Yellow Remove-PnPTermGroup -Identity $existingGroup -Force $existingGroup = $null } # Create group if it doesn't exist if (-not $existingGroup) { Write-Host "Creating term group: $($group.Name)" -ForegroundColor Cyan $newGroup = New-PnPTermGroup -Name $group.Name -Description $group.Description } else { $newGroup = $existingGroup Write-Host "Using existing term group: $($group.Name)" -ForegroundColor Cyan } # Restore each Term Set in the group foreach ($set in $group.TermSets) { $existingSet = Get-PnPTermSet -Identity $set.Name -TermGroup $newGroup -ErrorAction SilentlyContinue if ($existingSet -and $overwriteExisting) { Write-Host "Removing existing term set: $($set.Name) (overwrite enabled)" -ForegroundColor Yellow Remove-PnPTermSet -Identity $existingSet -Force $existingSet = $null } if (-not $existingSet) { Write-Host "Creating term set: $($set.Name)" -ForegroundColor Cyan $newSet = New-PnPTermSet -Name $set.Name -TermGroup $newGroup -Description $set.Description -LCID $set.Language } else { $newSet = $existingSet Write-Host "Using existing term set: $($set.Name)" -ForegroundColor Cyan } # Restore each Term in the set foreach ($term in $set.Terms) { $existingTerm = Get-PnPTerm -Identity $term.Name -TermSet $newSet -ErrorAction SilentlyContinue if ($existingTerm -and $overwriteExisting) { Write-Host "Removing existing term: $($term.Name) (overwrite enabled)" -ForegroundColor Yellow Remove-PnPTerm -Identity $existingTerm -Force $existingTerm = $null } if (-not $existingTerm) { Write-Host "Creating term: $($term.Name)" -ForegroundColor Cyan $newTerm = New-PnPTerm -Name $term.Name -TermSet $newSet -Description $term.Description -LCID $term.Language # Restore custom term properties (if any) if ($term.Properties) { foreach ($prop in $term.Properties) { Set-PnPTermProperty -Term $newTerm -Key $prop.Key -Value $prop.Value } } } else { Write-Host "Using existing term: $($term.Name)" -ForegroundColor Cyan } } } } Write-Host "Restore completed successfully!" -ForegroundColor Green } catch { Write-Host "Restore failed: $_" -ForegroundColor Red } finally { Disconnect-PnPOnline -ErrorAction SilentlyContinue }
Critical Restore Notes
- Test first: Always run a test restore on a non-production tenant or a dedicated test term group before touching your live environment.
- Overwrite warning: Setting
$overwriteExisting = $truewill delete existing groups/sets/terms that match the backup names. Only use this if you need a full replacement. - Multi-language support: This script handles the default language of terms. If you use multi-language terms, let me know and I can help extend the script to cover additional LCIDs.
Pro Tips
- Store backups in a secure, redundant location (like a network share or cloud storage) to avoid losing them.
- Keep at least 4 weeks of weekly backups for extra safety.
- After each backup, open the JSON file to verify it contains all your expected term data.
内容的提问来源于stack exchange,提问作者clare
相关产品推荐
相关产品推荐

