Powershell Script to Download Documents from Document Library


Follow a simple Powershell script to download all documents from a document library. It can be neat when you have large document libraries and don’t want to use Explorer View to transfer files.

 

$destination = “C:\\temp\\”
$web = Get-SPWeb -Identity “http://%5BSERVER NAME]/[SP WEB]”

$folderURL = “[DOCUMENT LIBRARY]/”

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()
}

foreach ($subfolder in $folder.SubFolders)
{
write-output $subfolder.Url
ProcessFolder($subfolder.Url)
}
}

ProcessFolder($folderURL)

 

See you,
Amadeu.

Advertisements

One Response to Powershell Script to Download Documents from Document Library

  1. allan says:

    beatiful codes, I tried it, very fast. thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: