Backing up SharePoint list data is a critical task to ensure data safety, compliance, and business continuity. PowerShell, combined with PnP PowerShell (Patterns & Practices), allows administrators to automate and streamline the backup process. In this guide, we will cover everything from installation to exporting and restoring SharePoint list data using PnP PowerShell.
Table of Contents
- Introduction to PnP PowerShell
- Installing and Configuring PnP PowerShell
- Connecting to SharePoint Online
- Exporting SharePoint List Data to CSV
- Backing Up List Attachments
- Backing Up List Schema (Structure)
- Restoring SharePoint List Data
- Automating the Backup Process
- Best Practices and Security Considerations
- Conclusion
1. Introduction to PnP PowerShell
PnP PowerShell is a powerful module developed by the SharePoint Patterns and Practices (PnP) community. It simplifies SharePoint administration and allows automation of various tasks, including data backup and restoration.
Why Use PnP PowerShell for Backup?
- Provides an easy-to-use command set for SharePoint Online and On-Premises.
- Supports authentication mechanisms for secure connections.
- Enables automation of repetitive tasks.
- Can be scheduled using Windows Task Scheduler or Azure Automation.
2. Installing and Configuring PnP PowerShell
Before backing up SharePoint list data, you must install and configure PnP PowerShell.
Step 1: Install PnP PowerShell
Run the following command in PowerShell (Run as Administrator):
powershellCopyEditInstall-Module -Name PnP.PowerShell -Scope CurrentUser
If you already have PnP PowerShell installed, update it to the latest version:
powershellCopyEditUpdate-Module -Name PnP.PowerShell
Step 2: Verify Installation
To check if PnP PowerShell is installed correctly, run:
Get-Module -Name PnP.PowerShell -ListAvailable
If it returns a valid version, PnP PowerShell is installed successfully.
3. Connecting to SharePoint Online
Before backing up data, establish a connection to your SharePoint site.
Step 1: Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
- Replace
yourtenant
with your actual SharePoint tenant name. - Replace
yoursite
with the name of the SharePoint site you want to back up. - The
-Interactive
switch will prompt for login credentials.
Alternatively, for automated scripts, use a stored credential:
$cred = Get-Credential
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Credentials $cred
4. Exporting SharePoint List Data to CSV
Exporting list data allows you to save it in a structured format.
Step 1: Define List Name and Export Path
$ListName = "YourListName"
$ExportPath = "C:\Backup\SharePointListBackup.csv"
Step 2: Retrieve List Items
$ListItems = Get-PnPListItem -List $ListName
Step 3: Convert Data to CSV Format
$Data = $ListItems | Select-Object FieldValues | ForEach-Object {
$_.FieldValues
} | Export-Csv -Path $ExportPath -NoTypeInformation
This script extracts all list items and exports them to a CSV file.
5. Backing Up List Attachments
List items may contain file attachments, which need to be backed up separately.
Step 1: Create a Backup Folder
$AttachmentBackupPath = "C:\Backup\Attachments"
New-Item -ItemType Directory -Path $AttachmentBackupPath -Force
Step 2: Export Attachments
$ListItems | ForEach-Object {
$ItemId = $_.Id
$Attachments = Get-PnPProperty -ClientObject $_ -Property Attachments
if ($Attachments) {
$Files = Get-PnPListItemAttachment -List $ListName -Id $ItemId
foreach ($File in $Files) {
$FilePath = "$AttachmentBackupPath\$ItemId-$($File.FileName)"
Get-PnPFile -Url $File.ServerRelativeUrl -Path $FilePath -FileName $File.FileName -AsFile
}
}
}
This script loops through each list item, checks for attachments, and downloads them.
6. Backing Up List Schema (Structure)
To restore the list structure later, export its schema as an XML file.
Step 1: Export List Schema
$SchemaPath = "C:\Backup\ListSchema.xml"
Get-PnPProvisioningTemplate -Out $SchemaPath -Handlers Lists
This command generates an XML file containing the list schema, including columns and settings.
7. Restoring SharePoint List Data
When required, restore data using the CSV backup.
Step 1: Read CSV Data
powershellCopyEdit$ImportPath = "C:\Backup\SharePointListBackup.csv"
$Data = Import-Csv -Path $ImportPath
Step 2: Restore Items to SharePoint List
foreach ($Item in $Data) {
Add-PnPListItem -List "YourListName" -Values @{
"Title" = $Item.Title
"Column1" = $Item.Column1
"Column2" = $Item.Column2
}
}
Modify the column names according to your list structure.
8. Automating the Backup Process
Automate the backup script using Windows Task Scheduler or Power Automate.
Step 1: Save Script as a PowerShell File
Save the backup script as Backup-SharePoint.ps1
.
Step 2: Create a Scheduled Task
- Open Task Scheduler.
- Click Create Basic Task.
- Set Trigger (Daily, Weekly, or Custom).
- Set Action as Start a Program.
- Browse to
Backup-SharePoint.ps1
and selectpowershell.exe
to run the script. - Save and Enable the Task.