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 straight forward by Clicking "Download a Copy" from 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 ""
#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?


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

  2. Thanks!!! This helped a lot!!!

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

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

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

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

  6. 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?

  7. 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.

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

  9. 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


Please Login and comment to get your questions answered!

Powered by Blogger.