Migrating files to OneDrive for Business is a critical task for organizations moving to the cloud. PnP PowerShell provides a seamless way to upload bulk files from local storage or SharePoint to OneDrive efficiently.
In this guide, you’ll learn how to:
Connect to OneDrive using PnP PowerShell
Upload files from a local folder to OneDrive
Migrate an entire folder to OneDrive
Verify and manage the migration
Step 1: Install and Import PnP PowerShell
Ensure that PnP PowerShell is installed. If not, install it:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Import the module:
powershellCopyEditImport-Module PnP.PowerShell
PnP PowerShell is ready!
Step 2: Connect to OneDrive
To manage OneDrive, first connect using PnP PowerShell:
$OneDriveUrl = "https://yourtenant-my.sharepoint.com/personal/your_email_com"
Connect-PnPOnline -Url $OneDriveUrl -Interactive
🔹 Replace yourtenant with your Microsoft 365 tenant name.
🔹 Replace your_email_com with your OneDrive account UPN format.
🔹 This command will prompt for Microsoft 365 login credentials.
Connected to OneDrive!
Step 3: Upload a Single File to OneDrive
To upload a file from your local PC to a OneDrive folder:
$LocalFilePath = "C:\Users\YourName\Documents\Report.pdf"
$OneDriveFolderPath = "/Documents"
Add-PnPFile -Path $LocalFilePath -Folder $OneDriveFolderPath
🔹 Replace C:\Users\YourName\Documents\Report.pdf with the path to your file.
🔹 Replace /Documents with the target OneDrive folder.
Single file uploaded!
Step 4: Upload Multiple Files to OneDrive
To migrate multiple files from a folder:
$LocalFolderPath = "C:\Users\YourName\Documents\MigrateFiles"
$OneDriveFolderPath = "/Documents"
$Files = Get-ChildItem -Path $LocalFolderPath -File
foreach ($File in $Files) {
$FilePath = $File.FullName
Add-PnPFile -Path $FilePath -Folder $OneDriveFolderPath
}
🔹 This script loops through all files in C:\Users\YourName\Documents\MigrateFiles and uploads them to OneDrive.
Bulk file migration completed!
Step 5: Migrate an Entire Folder to OneDrive
To create a folder in OneDrive and upload its contents:
$LocalFolderPath = "C:\Users\YourName\Documents\ProjectFiles"
$OneDriveFolderPath = "/ProjectFiles"
# Create the folder in OneDrive
New-PnPFolder -Name "ProjectFiles" -Folder "/"
# Upload all files inside the folder
$Files = Get-ChildItem -Path $LocalFolderPath -File
foreach ($File in $Files) {
$FilePath = $File.FullName
Add-PnPFile -Path $FilePath -Folder $OneDriveFolderPath
}
Folder and its contents migrated to OneDrive!
Step 6: Verify the Migration
To check the uploaded files in OneDrive:
Get-PnPListItem -List "Documents"
This lists all items in the Documents folder.
Migration verification completed!
Step 7: Disconnect the Session
Once done, disconnect from OneDrive:
Disconnect-PnPOnline
Session closed successfully!