Home » Blog » SharePoint Online » How to Download Folder from SharePoint Online Using PowerShell? A Complete Guide

How to Download Folder from SharePoint Online Using PowerShell? A Complete Guide

author
Published By Raj Kumar
Anuraag Singh
Approved By Anuraag Singh
Published On September 9th, 2024
Reading Time 5 Minutes Reading

Are you looking for an efficient solution for how to download folder from SharePoint Online using PowerShell? If yes, you have found the right place. Here we will cover essential approaches, best and practices to ensure a smooth download process.

SharePoint users download files and folders from the document library daily. While SharePoint provides the basic option to download files and folders flawlessly. Besides the SharePoint basic option, there is also a way to download SharePoint files and folders using PowerShell.

So, let’s begin the process of downloading SharePoint document files and folders.

How to Download Folder(Along with Incorporated Files) from SharePoint Online Using Admin Center?

Ensure you have the necessary permissions to access the document library, because sometimes admins restrict the permissions to prevent users from downloading files in SharePoint.

Step 1. After login to SharePoint Online using the appropriate credentials. Then Navigate, to the SharePoint Online site collection.
Step 2. Open the Document Library from where you want to download the folder.
Step 3. Choose the Folder and press right-click. Now hit the Download option to download the folder along with their files.
Step 4. If you want to download multiple folders, then select the folders and click on the Download option from the top nav bar.

[Note:- It will save all the folders into a zip file.]

Shortcomings

  • There are several shortcomings in downloading SharePoint files and folders using Admin Center such as.
  • The maximum file you can download is 250GB.
  • There is a limit of 10,000 files to download only.
  • A maximum of 10,000 files per folder is permitted only.

How to Download Folder from SharePoint Online Using PowerShell?

You can execute the below commands, only if you are good at the PowerShell scripts. Using these commands, you can automate the downloading process of SharePoint folders.

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Download-Folder()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [string] $TargetFolder
)
Try {

$NameofFolder = ($SourceFolder.ServerRelativeURL) -replace "/","\"
$LocalFolderLoc = $TargetFolder + $NameofFolder
If (!(Test-Path -Path $LocalFolderLoc)) {
New-Item -ItemType Directory -Path $LocalFolderLoc | Out-Null
}

$FilesColl = $SourceFolder.Files
$Ctx.Load($FilesColl)
$Ctx.ExecuteQuery()

Foreach($File in $FilesColl)
{
$TargetedFile = $LocalFolderLoc+"\"+$File.Name

$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$File.ServerRelativeURL)
$WriteStream = [System.IO.File]::Open($TargetedFile,[System.IO.FileMode]::Create)
$FileInfo.Stream.CopyTo($WriteStream)
$WriteStream.Close()
write-host -f Green "Downloaded File:"$TargetedFile
}

$SubFolders = $SourceFolder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
Foreach($Folder in $SubFolders)
{
If($Folder.Name -ne "")
{

Download-Folder -SiteURL $SiteURL -SourceFolder $Folder -TargetFolder $TargetFolder
}
}
}
Catch {
write-host -foregroundcolor Red "Error while Downloading Folder!" $_.Exception.Message
}
}

$SiteURL=""
$FolderRelativeUrl =""
$TargetFolder="C:\Docs"

$Credt= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credt.Username, $Credt.Password)

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
$Web.ServerRelativeUrl+$FolderRelativeUrl

$SourceFolder = $Web.GetFolderByServerRelativeUrl($Web.ServerRelativeUrl+$FolderRelativeUrl)
$Ctx.Load($SourceFolder)
$Ctx.ExecuteQuery()
Download-Folder -SiteURL $SiteURL -SourceFolder $SourceFolder -TargetFolder $TargetFolder

Download Folder, Subfolder, and Files from SharePoint Document Library

You can also use this well-optimized script to download folder from SharePoint Online using PowerShell script without any hurdles.

#Set Parameters
$SiteURL = ""
$FolderServerRelativeURL = "”
$DownloadPath ="C:\Docs\New"

Connect-PnPOnline -Url $SiteURL -Interactive
$Web = Get-PnPWeb

$FoldertoDownload = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.ParentList
#Get the Folder's Site Relative URL
$SiteRelativeURLofFolder = $FolderServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length)

$List = $Folder.ListItemAllFields.ParentList

$global:counter = 0;
$ListItems = Get-PnPListItem -List $List -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `
($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Items from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";} | Where {$_.FieldValues.FileRef -like "$($FolderServerRelativeUrl)*"}
Write-Progress -Activity "Completed Retrieving Items from Folder $FolderServerRelativeURL" -Completed

$AllSubFolders = $ListItems | Where {$_.FileSystemObjectType -eq "Folder" -and $_.FieldValues.FileLeafRef -ne "Forms"}
$AllSubFolders | ForEach-Object {

$LocalStoredFolder = $DownloadPath + ($_.FieldValues.FileRef.Substring($Web.ServerRelativeUrl.Length)) -replace "/","\"

If (!(Test-Path -Path $LocalStoredFolder)) {
New-Item -ItemType Directory -Path $LocalStoredFolder | Out-Null
}
Write-host -f Yellow "Ensured Folder '$LocalStoredFolder'"
}

$FilesColl = $ListItems | Where {$_.FileSystemObjectType -eq "File"}
#Iterate through each file and download
$FilesColl | ForEach-Object {
$FileDownloadPath = ($DownloadPath + ($_.FieldValues.FileRef.Substring($Web.ServerRelativeUrl.Length)) -replace "/","\").Replace($_.FieldValues.FileLeafRef,'')
Get-PnPFile -ServerRelativeUrl $_.FieldValues.FileRef -Path $FileDownloadPath -FileName $_.FieldValues.FileLeafRef -AsFile -force
Write-host -f Green "Downloaded all Files from '$($_.FieldValues.FileRef)'"
}

Best Practices to Save SharePoint Files and Folders Locally

  • Check Permissions: Ensure you have the required permissions to download the folders and their files.
  • Verify Folder Path: Validate the folder path before going to execute the PowerShell scripts.
  • Review Error Messages: While executing the PowerShell commands, monitor the process and review the error messages.

If you are downloading the SharePoint folders and files for moving them to another SharePoint account. Then you can also use the Most Prominent SharePoint Migration Tool. This tool is capable enough to copy document library from one site to another in SharePoint.

Download Now Purchase Now

If you choose this professional tool to perform migration. Then you do not need to download the folders using PowerShell. You can directly shift them to the destination account with these quick steps.

  • Step 1. Download and Setup Tool.
  • Step 2. Select Source & Destination accounts.
  • Step 3. Choose Sites and complete account details.
  • Step 4. Add users and then Start Migration.

Conclusion

Downloading files and folders from SharePoint Online is a common task for SharePoint users. Therefore, we have explained the numerous methods to download folder from SharePoint Online using PowerShell or Admin Center. An efficient tool is also elaborated to simplify the migration process if required. Hence the query is resolved.

Connect With Us

+9111-28084986