Download All Files From a SharePoint Library Programmatically using PowerShell

Ever wanted to download all files from a SharePoint Library (Document Library, Picture Library, etc.) to your local drive?

While downloading a single file or document is fairly straightforward by clicking “Download a Copy” from the documents tab of the Ribbon or From the ECB Menu of the document  >> Send To >> “Download a Copy”, downloading Multiple files or a complete folder is not that easy when the files count is high. Sure, Explorer view can do it! Go to the Document Library and choose Explorer View from the Ribbon

Download All Files From a SharePoint Library Programmatically using PowerShell

Now you can drag & Drop (or Copy & Paste) folders, Files from and to your local drive from SharePoint. You can also use map your SharePoint libraries to network drive. 

But How about downloading all documents from all document libraries? Yes, PowerShell can do it well!

PowerShell Script to download all files and folders from a SharePoint Library along with its Structure:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Function to Download All Files from a SharePoint Library
Function DownloadFiles($SPFolderURL, $LocalFolderPath)
{
        #Get the Source SharePoint Folder
        $SPFolder = $web.GetFolder($SPFolderURL)

        $LocalFolderPath = Join-Path $LocalFolderPath $SPFolder.Name 
        #Ensure the destination local folder exists! 
        if (!(Test-Path -path $LocalFolderPath))
        {    
             #If it doesn't exist, Create
             $LocalFolder = New-Item $LocalFolderPath -type directory 
        }

     #Loop through each file in the folder and download it to Destination
     foreach ($File in $SPFolder.Files) 
     {
         #Download the file
         $Data = $File.OpenBinary()
         $FilePath= Join-Path $LocalFolderPath $File.Name
         [System.IO.File]::WriteAllBytes($FilePath, $data)
     }

     #Process the Sub Folders & Recursively call the function
     foreach ($SubFolder in $SPFolder.SubFolders)
     {
           if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder
             {
                  #Call the function Recursively
                  DownloadFiles $SubFolder $LocalFolderPath
             }
     }
 
}

#Get the Source Web
$Web = Get-SPWeb "https://sharepoint.crescent.com/sites/Operations"
 
#Get the Source SharePoint Library's Root Folder
$SourceLibrary =  $Web.Lists["Design Documents"].RootFolder

#Local Folder, where the Files to be downloaded 
$DestinationPath = "C:\Test" 

#Call the Download Files Function
DownloadFiles $SourceLibrary $DestinationPath

The above code is pretty simple. We can re-write this in C# Object model code, Add a Custom Ribbon item to download all files. If you want to download all document libraries from a SharePoint site, use my another article: How to Download All Files in SharePoint Document Library using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

12 thoughts on “Download All Files From a SharePoint Library Programmatically using PowerShell

  • the script change all the windows native metadata.. for ex. modified date ..etc if I need to keep metadata intact while downloading the file from library.. any suggestion

    Reply
  • Is there any size limit for this to work? With very large libraries will it work?

    Reply
  • I need to download set of documents that located in different libraries to a network location.
    I have the list of file paths in a excel file. I would like to read in that file and download the documents.How to accomplish this task. Thank you for you help.

    Reply
  • Thanks Salaudeen, i am trying to download all in network drive(network locations) instead of disk on server but its failing(working in disk/drive C: or E:), any thoughts?

    Reply
  • Hi, Great script, how would i go about it if i wanted to download an entire site collection as is?

    Reply
  • How to delete everything in source document library after everything is copied?

    Reply
  • Thanks! If we want to only download from document libraries subfolder to local drive? how to change script? Please advise.

    Reply
    • E.g. You want to get files from “Sales” Sub-folder of “Design Documents” Library, Use:
      $SourceLibrary = $Web.Lists[“Design Documents”].RootFolder.SubFolders[“Sales”];

      Reply
  • Thanks!!! This helped a lot!!!

    Reply
  • Its really great work.

    Reply
  • Thanks! An elegant script, and exactly what I needed.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *