Monday, June 24, 2013

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.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

2 comments :

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

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...