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 "http://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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • March 1, 2021 at 11:25 PM

    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
  • November 19, 2020 at 6:30 AM

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

    Reply
  • May 19, 2020 at 3:27 PM

    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
  • September 12, 2019 at 2:21 PM

    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
  • October 24, 2018 at 10:26 AM

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

    Reply
  • September 5, 2018 at 5:10 PM

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

    Reply
  • April 30, 2018 at 3:34 AM

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

    Reply
    • April 30, 2018 at 9:55 AM

      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
  • March 22, 2017 at 8:33 AM

    Thanks!!! This helped a lot!!!

    Reply
  • October 14, 2015 at 5:23 PM

    Its really great work.

    Reply
  • February 17, 2015 at 8:42 PM

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

    Reply

Leave a Reply