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

  1. Connect to OneDrive: The script starts by connecting to your OneDrive for Business account using the Connect-PnPOnline cmdlet.
  2. Retrieve Files and Folders: It then retrieves the list of files and folders from the specified document library.
  3. 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.

Adnan, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply