kwizcom banner advertisement

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 exists, 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.
Download All Files From a SharePoint Library Programmatically using PowerShell Download All Files From a SharePoint Library Programmatically using PowerShell Reviewed by Salaudeen Rajack on June 24, 2013 Rating: 5

6 comments:

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

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

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

    ReplyDelete
    Replies
    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"];

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

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.