CHunky Universe of Vigourous Astonishing SHarepoint :)

PowerShell: Copy an entire document library from SharePoint 2007 to disk

For a while ago I needed to copy all files from a document library within a SharePoint 2007 site to the hard drive. So I didn’t need to copy files from SharePoint to SharePoint so I couldn’t use the stsadm -o export command or Chris O’Brien’s nice SharePoint Content Deployment Wizard. I came across the SPIEFolder application which should work with SharePoint 2007 and 2010. It has a site on codeplex:, but neither the binary nor the source code can be downloaded from there. After some searching I found the binary in the author’s skydrive. The fact that the source code was not available seemed as an disanvantage because I could not know what code was run. Nevertheless I tried it out and it didn’t work:

spiefolder -o export -url "http://dev/Documents" -directory c:\tolle\Documents –recursive

I got the following error:

The Web application at http://dev/Documents could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

So I wrote my own code to copy the documents. To write a console application feels so yesterdayish, so it is written in PowerShell. Even if there are no PowerShell snapins for SharePoint 2007, you have access to the entire Server Object Model, the only thing you have to do is to load the SharePoint assembly:


Then you can instantiate all SharePoint objects like in C#, but in a PowerShell way:

$site = new-Object Microsoft.SharePoint.SPSite("http://dev")
$web = $site.OpenWeb()

You can even download a module for emulating cmdlets: Get-SPWeb, Get-SPWebApplication and Get-SPFarm, written by Natalia Tsymbalenko ( to get started or just to find some inspiration.

I have created a ps1-script which only does one thing – it copies an entire document library to disk. Much of inspiration to structure the script comes from “Delete-SPListItems” (

Here it is: Pull-Documents.ps1

    Use Pull-Documents to copy the entire document library to disk
    This script iterates recursively over all directories and files in a document library and writes binary data to the disk
    The structure is kept as in the Document library
    It is mainly written for SharePoint 2007, but it works even in SharePoint 2010
    Pull-Document -Url http://dev -Library "Shared Documents"
    Name: Pull-Documents.ps1
    Author: Anatoly Mironov
    Last Edit: 2012-12-03
    Keywords: SPList, Documents, Files, SPDocumentLibrary
#Requires -Version 1.0
[Parameter(Mandatory=$true)][System.String]$Url = $(Read-Host -prompt "Web Url"),
[Parameter(Mandatory=$true)][System.String]$Library = $(Read-Host -prompt "Document Library")

$site = new-object microsoft.sharepoint.spsite($Url)
$web = $site.OpenWeb()

$folder = $web.GetFolder($Library)
$folder # must output it otherwise "doesn't exist" in 2007

    Write-Error "The document library cannot be found"

$directory = $pwd.Path

$rootDirectory = Join-Path $pwd $folder.Name

if (Test-Path $rootDirectory) {
    Write-Error "The folder $Library in the current directory already exists, please remove it"

#progress variables
$global:counter = 0
$global:total = 0
#recursively count all files to pull
function count($folder) {
    if ($folder.Name -ne "Forms") {
        $global:total += $folder.Files.Count
        $folder.SubFolders | Foreach { count $_ }
write "counting files, please wait..."
count $folder
write "files count $global:total"

function progress($path) {
    $percent = $global:counter / $global:total * 100
    write-progress -activity "Pulling documents from $Library" -status $path -PercentComplete $percent

#Write file to disk
function Save ($file, $directory) {
    $data = $file.OpenBinary()
    $path = Join-Path $directory $file.Name
    progress $path
    [System.IO.File]::WriteAllBytes($path, $data)

#Forms folder doesn't need to be copied
$formsDirectory = Join-Path $rootDirectory "Forms"

function Pull($folder, [string]$directory) {
    $directory = Join-Path $directory $folder.Name
    if ($directory -eq $formsDirectory) {
    mkdir $directory | out-null

    $folder.Files | Foreach { Save $_ $directory }

    $folder.Subfolders | Foreach { Pull $_ $directory }

Write "Copying files recursively"
Pull $folder $directory


I have tested this script in SharePoint 2007 and 2010. It works. Let me know if you find this useful or have some suggestions.

14 responses to “PowerShell: Copy an entire document library from SharePoint 2007 to disk

  1. Ryan Dennis 2012-12-11 at 20:35

    Reblogged this on and commented:
    Great post!

    • Anatoly Mironov 2012-12-11 at 21:09

      Thanks Ryan! I hope it can help someone in the SharePoint community.I have got much help from the community in my SharePoint work, among them your awesome blog. I want to share my findings with the sharepoint community as well.

  2. Anant 2013-05-08 at 12:48

    Hi Anatoly,
    I have some large sites that have very large document libraries and some of them are as big as 32 GB. Do you know if there are any limits as to how big doc library we can copy/export from SharePoint and then import into another site collection?

    • Anatoly Mironov 2013-05-08 at 20:35

      Hi Anant. It should be no limitations. This script iterates recursively through a whole document library and saves the files on disk, file after file. If you want to to save the files to another document library, probably it should be better to change the script and copy files directly to the target doc lib.

  3. Shival Khanna 2013-05-12 at 18:28

    Hi Anatoly, This script is very useful. It worked like a charm.. Thanks for sharing it.

  4. Robyn Farley 2014-07-30 at 19:21

    Awesome! Is it possible to redirect the output to a specific location?

  5. Ambrose 2014-10-09 at 05:02

    I like what you guys are usually up too. Such clever ork annd exposure!
    Keep up the excellent works guys I’ve added you uys to
    my oown blogroll.

  6. krishna 2014-11-12 at 06:00

    Hi Anatoly, could please help me to understand,
    Where these files stores in the file system.

  7. Robyn Gwinn 2015-09-15 at 19:12

    Is there a way to do the reverse of this script — copy folders and files within the folders from a shared drive into a Sharepoint 2007 document library?

  8. Lori 2016-01-21 at 18:07

    Is there a way to edit this so that I would be able to input all URLs and Libraries through a txt file that lists all of them? If so, how would I do this? I have many sites that I have to export documents from and don’t want to have to add all of these sites and libraries individually.

  9. Tess 2017-01-16 at 03:28

    Will this maintain the content type of the documents if I then upload them to SharePoint 13?

  10. Steve Webster 2017-11-01 at 16:49

    Thank you 🙂 You have saved me a ton of work.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

Daniel Chronlund Cloud Tech Blog

News, tips and thoughts for Microsoft cloud fans

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Architect, Speaker, Author

Microsoft 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Mai Omar Desouki

PFE @ Microsoft

Cameron Dwyer

Office 365, SharePoint, Azure, OnePlace Solutions & Life's Other Little Wonders


Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

DevOps, Cloud and Blockchain Consultant


SharePoint for everyone


Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint


Mostly what I know and share about...

%d bloggers like this: