
In today’s fast-paced work environment, efficiency and automation are key. For those utilizing OneDrive for Business, automating the process of downloading your files can save a significant amount of time. In this blog post, we’ll explore a PowerShell script that simplifies the process of transferring files and folders from OneDrive for Business directly to your local machine.
PowerShell script to Download OneDrive files locally
The script provided below is designed to connect to your OneDrive for Business account and download all the files and folders to a specified local directory. It uses the PnP PowerShell module, which is a powerful set of cmdlets allowing you to perform complex SharePoint and OneDrive operations from the command line.
###########################################################################
#Author: Adnan Amin
#blog: Https://mstechtalk.com
#Downloading files from OneDrive to local machine
###########################################################################
$webUrl = "https://mstalk-my.sharepoint.com/personal/<user>_mstechtalk_com";
$listUrl = "Documents";
$destination = "D:\test"
#Connect-PnPOnline -Url $webUrl
Connect-PnPOnline -Url $webUrl -Interactive
$web = Get-PnPWeb
$list = Get-PNPList -Identity $listUrl
function ProcessFolder($folderUrl, $destinationFolder) {
$folder = Get-PnPFolder -RelativeUrl $folderUrl
Get-PnPProperty -ClientObject $folder -Property Files
if (!(Test-Path -path $destinationfolder)) {
$dest = New-Item $destinationfolder -type directory
}
$folder.Files | ForEach-Object {
try {
Write-Verbose "Copying file $($_.Name) at $destinationfolder"
Get-PnPFile -ServerRelativeUrl $_.ServerRelativeUrl -Path $destinationfolder -FileName $_.Name -AsFile
} catch {
Write-Error "Failed to download file $($_.Name): $_"
}
}
}
function ProcessSubFolders($folders, $currentPath) {
foreach ($folder in $folders) {
#Avoid Forms folders
if ($folder.Name -ne "Forms") {
$targetFolder = $currentPath +"\"+ $folder.Name;
try {
ProcessFolder $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length) $targetFolder
$tempfolders = Get-PnPProperty -ClientObject $folder -Property Folders
Write-Verbose "Processing folder: $folder.Name .. at $currentPath"
ProcessSubFolders $tempfolders $targetFolder
} catch {
Write-Error "Failed to process folder $folder.Name: $_"
}
}
}
}
ProcessFolder $listUrl $destination + "\"
#Write-Host "listUrl: " $listUrl
#Download files in folders
$tempfolders = Get-PnPProperty -ClientObject $list.RootFolder -Property Folders
Write-Host "tempfolders: " $tempfolders
ProcessSubFolders $tempfolders $destination + "\"
Step-by-Step
- Connect to OneDrive: The script starts by connecting to your OneDrive for Business account using the Connect-PnPOnline cmdlet.
- Retrieve Files and Folders: It then retrieves the list of files and folders from the specified document library.
- Download Process: The ProcessFolder and ProcessSubFolders functions work together to download each item, maintaining the folder structure.
Handling Errors
The script includes error handling to ensure that any issues encountered during the download process are logged, allowing you to troubleshoot and resolve them effectively.
Automating your OneDrive for Business downloads with PowerShell is not only a time-saver but also introduces a level of precision and reliability to your file management tasks. Give this script a try and experience the convenience of automated downloads.






No Comments