I was asked to download a lot of files from document library. Of course, I could open document library in Explore mode, but there was a lot of files and it could take a long time. So I’ve found a good PowerShell script to save files from document library.

It's not my code, it's just good 🙂 Here it is:

######################## Start Variables ######################## 
$destination = "C:\\Data\\DCData" 
$webUrl = "<Url of the specific site>" 
$listUrl = "<Url of the specific list. This url is complete Url and NOT relative"
############################################################## 

$web = Get-SPWeb -Identity $webUrl 
$list = $web.GetList($listUrl) 

function ProcessFolder { 
param($folderUrl) 
$folder = $web.GetFolder($folderUrl) 
foreach ($file in $folder.Files) { 
#Ensure destination directory 
$destinationfolder = $destination + "/" + $folder.Url 
if (!(Test-Path -path $destinationfolder)) 
{ 
$dest = New-Item $destinationfolder -type directory 
} 
#Download file 
$binary = $file.OpenBinary() 
$stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create 
$writer = New-Object System.IO.BinaryWriter($stream) 
$writer.write($binary) 
$writer.Close() 
} 
} 

#Download root files 
ProcessFolder($list.RootFolder.Url) 
#Download files in folders 
foreach ($folder in $list.Folders) { 
ProcessFolder($folder.Url) 
} 

This code reads binary from each element of document library and writes binary file on disc.