Automating OneDrive Backup and Restore using PnP PowerShell

Loading

Backing up and restoring OneDrive for Business is critical to protect important data from accidental deletions, ransomware attacks, or user errors. With PnP PowerShell, you can automate the backup and restore process, ensuring data integrity and security.

This guide will cover:
Backing up OneDrive files to a local or cloud location
Restoring OneDrive files from backup
Automating the process using PowerShell scripts


Prerequisites

1️⃣ Install PnP PowerShell

Ensure that PnP PowerShell is installed on your system:

Install-Module -Name PnP.PowerShell -Force -AllowClobber

2️⃣ Connect to OneDrive

Before performing any operations, authenticate to OneDrive for Business:

Connect-PnPOnline -Url "https://yourtenant-my.sharepoint.com" -Interactive

Replace "yourtenant" with your actual SharePoint tenant name.

Use Global Administrator or SharePoint Administrator credentials.


Step 1: Backup OneDrive Files to a Local System

To download all files from OneDrive and save them locally:

# Define variables
$oneDriveFolder = "Documents"
$backupPath = "C:\OneDriveBackup"

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

# Connect to OneDrive
Connect-PnPOnline -Url "https://yourtenant-my.sharepoint.com" -Interactive

# Get all files in the OneDrive folder
$files = Get-PnPListItem -List $oneDriveFolder

# Download each file
foreach ($file in $files) {
$fileUrl = $file.FieldValues["FileRef"]
$fileName = Split-Path -Leaf $fileUrl
$localFilePath = Join-Path -Path $backupPath -ChildPath $fileName

Write-Host "Downloading: $fileName" -ForegroundColor Green
Get-PnPFile -Url $fileUrl -Path $localFilePath -AsFile
}

This script downloads all files from OneDrive and stores them in C:\OneDriveBackup.
It maintains the original file names and structure.


Step 2: Backup OneDrive Files to Another Cloud Storage (SharePoint or Azure Blob Storage)

If you want to store backups in SharePoint, use:

# Define variables
$oneDriveFolder = "Documents"
$sharePointBackupFolder = "/Backup"

# Connect to OneDrive
Connect-PnPOnline -Url "https://yourtenant-my.sharepoint.com" -Interactive

# Get all files from OneDrive
$files = Get-PnPListItem -List $oneDriveFolder

# Upload files to SharePoint Backup Folder
foreach ($file in $files) {
$fileUrl = $file.FieldValues["FileRef"]
$fileName = Split-Path -Leaf $fileUrl

Write-Host "Backing up: $fileName to SharePoint" -ForegroundColor Green
Add-PnPFile -Path $fileUrl -Folder $sharePointBackupFolder
}

This script copies all OneDrive files to a SharePoint backup folder.
Ensures a cloud-based backup for added security.


Step 3: Restore OneDrive Files from Backup

To restore backed-up files to OneDrive, use:

# Define variables
$backupPath = "C:\OneDriveBackup"
$oneDriveFolder = "/Documents"

# Connect to OneDrive
Connect-PnPOnline -Url "https://yourtenant-my.sharepoint.com" -Interactive

# Upload all files from backup to OneDrive
Get-ChildItem -Path $backupPath -File | ForEach-Object {
$file = $_.FullName
Write-Host "Restoring: $($_.Name)" -ForegroundColor Green
Add-PnPFile -Path $file -Folder $oneDriveFolder
}

Restores files from a local backup to OneDrive.
Can be used to recover lost or deleted files.


Step 4: Automating the Backup Process

To automate OneDrive backups, schedule the script to run daily using Task Scheduler:

1️⃣ Open Task Scheduler (taskschd.msc).
2️⃣ Click Create Basic Task → Name it OneDrive Backup.
3️⃣ Set Trigger to Daily or Weekly.
4️⃣ Choose ActionStart a Program.
5️⃣ Browse and select PowerShell (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe).
6️⃣ Add script path in the Arguments field:

-ExecutionPolicy Bypass -File "C:\Scripts\OneDriveBackup.ps1"

7️⃣ Click Finish.

Now your OneDrive will automatically back up on a schedule!


Step 5: Monitoring Backup and Restore Status

To verify the last backup time:

$backupPath = "C:\OneDriveBackup"
Get-ChildItem -Path $backupPath | Select-Object Name, LastWriteTime

Displays file names and last modified timestamps.


Step 6: Handling OneDrive Recycle Bin for Deleted Files

If files are accidentally deleted, restore them from the OneDrive Recycle Bin:

# Get deleted files
$deletedItems = Get-PnPRecycleBinItem

# Restore all deleted files
$deletedItems | ForEach-Object {
Write-Host "Restoring: $($_.Title)" -ForegroundColor Green
Restore-PnPRecycleBinItem -Identity $_.Id
}

Recovers all deleted files.


Step 7: Check OneDrive Storage Usage Before Backup

Before running a backup, check available storage:

Get-PnPStorageEntity | Where-Object { $_.Key -eq "StorageQuota" }

Ensures OneDrive has enough free space for backup.

Leave a Reply

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