
OneDrive for Business stores files in SharePoint Online document libraries, making it possible to bulk download files using PnP PowerShell. This guide covers:
 Connecting to OneDrive
 Retrieving files from a specific folder
 Downloading all files
 Downloading files based on conditions
Step 1: Install and Import PnP PowerShell
If you haven’t installed PnP PowerShell, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Import the module:
Import-Module PnP.PowerShell
PnP PowerShell is ready!
Step 2: Connect to OneDrive
First, establish a connection to your OneDrive site:
$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 user’s UPN (User Principal Name).
🔹 This will prompt you for Microsoft 365 login credentials.
Connected to OneDrive!
Step 3: Retrieve a List of All Files
To list all files in your OneDrive:
$Files = Get-PnPListItem -List "Documents"
$Files | Select-Object FileLeafRef, FileRef
🔹 This retrieves all files from the Documents library.
🔹 The output will show FileLeafRef (file name) and FileRef (file path).
Retrieved file list!
Step 4: Bulk Download All Files
To download all files to a local folder:
$LocalPath = "C:\OneDriveDownloads"
New-Item -ItemType Directory -Path $LocalPath -Force
$Files = Get-PnPListItem -List "Documents"
foreach ($File in $Files) {
    $FileUrl = $File["FileRef"]
    $FileName = $File["FileLeafRef"]
    $Destination = "$LocalPath\$FileName"
    
    Get-PnPFile -Url $FileUrl -Path $LocalPath -FileName $FileName -AsFile -Force
}
Write-Host "Download completed! Files are saved in $LocalPath"
🔹 Creates a local folder (C:\OneDriveDownloads).
🔹 Loops through all files in OneDrive and downloads them.
🔹 Uses -Force to overwrite existing files.
Bulk files downloaded!
Step 5: Bulk Download Files from a Specific Folder
If you want to download files from a specific OneDrive folder, modify the script:
$FolderPath = "/Documents/Reports"  # Change to your OneDrive folder path
$LocalPath = "C:\OneDriveDownloads\Reports"
New-Item -ItemType Directory -Path $LocalPath -Force
$Files = Get-PnPListItem -List "Documents" | Where-Object { $_["FileRef"] -like "$FolderPath/*" }
foreach ($File in $Files) {
    $FileUrl = $File["FileRef"]
    $FileName = $File["FileLeafRef"]
    Get-PnPFile -Url $FileUrl -Path $LocalPath -FileName $FileName -AsFile -Force
}
Write-Host "Files from $FolderPath downloaded to $LocalPath"
🔹 Updates $FolderPath to specify a OneDrive folder.
🔹 Downloads only files inside that folder.
Downloaded files from a specific OneDrive folder!
Step 6: Download Only Specific File Types
To download only certain file types (e.g., PDFs and Word documents):
$LocalPath = "C:\OneDriveDownloads"
$Extensions = @(".pdf", ".docx") # Add more extensions if needed
New-Item -ItemType Directory -Path $LocalPath -Force
$Files = Get-PnPListItem -List "Documents" | Where-Object { $_["FileLeafRef"] -match "\.pdf|\.docx$" }
foreach ($File in $Files) {
    $FileUrl = $File["FileRef"]
    $FileName = $File["FileLeafRef"]
    Get-PnPFile -Url $FileUrl -Path $LocalPath -FileName $FileName -AsFile -Force
}
Write-Host "Only PDF and DOCX files downloaded!"
🔹 Filters files by extension before downloading.
Downloaded specific file types!
Step 7: Verify Downloaded Files
To check the downloaded files in local storage:
Get-ChildItem -Path "C:\OneDriveDownloads" | Select Name, Length
🔹 Lists the downloaded files with their sizes.
Verified files in local storage!
Step 8: Disconnect the Session
Once you’re done, disconnect the session:
Disconnect-PnPOnline
Disconnected from OneDrive!
