Share via


SharePoint 2010 | Powershell | Download all files in document library to Network Share (File share)

I was recently working on a client requirement to download all the content of a list/document library to a file share. This script should ensure the folder structure of the document library to be replicated in the file share.

 ######################## Start Variables ########################
######################## Varun's Script######################
$destination = "C:\\tools\\Folder"
$webUrl = "<Url of the specific site>"
$listUrl = "<Url of the specific list. This url is complete Url and NOT relative url>"
##############################################################

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

Comments

  • Anonymous
    February 07, 2012
    Very cool. I hope all of this is ending up in the Technet gallary!

  • Anonymous
    February 08, 2012
    I was looking for this for long, thanks.

  • Anonymous
    July 20, 2012
    The comment has been removed

  • Anonymous
    September 05, 2012
    does it copy versions of a document also?

  • Anonymous
    September 10, 2012
    The code spills out of the right marging for some lines ...

  • Anonymous
    September 14, 2012
    Varun, Do you have any PS Script to do reverse of this process. I have requirement to automate the process of move the files from fileshare to SharePoint document library. Please help me if you have any scripts. Appreciate your help. Thanks Meera

  • Anonymous
    September 26, 2012
    Meera, i used this tool and it was perfect: OrbitOne.SharePoint.Importer. You have to configure some config files. Try it

  • Anonymous
    April 10, 2013
    Any body tried this for SP 2013 ? any different ?

  • Anonymous
    June 24, 2013
    Can we move it to a dynamic folder which gets created on the local or network drive, based on the site name and document library name?

  • Anonymous
    June 18, 2014
    Hi, very nice and usefull script. Is there any chance to use it for several libraries? Or I have to create a script for each library I need to copy? Thanks

  • Anonymous
    July 29, 2014
    how do we delete a document from SharePoint after it is exported?

  • Anonymous
    October 01, 2014
    Exactly what we needed!  Thanks for sharing this.  Cheers!

  • Anonymous
    January 16, 2015
    Excellent Varun!!.. Helped me a lot....

  • Anonymous
    January 16, 2015
    How can permissions be applied as ACLs?

  • Anonymous
    January 28, 2015
    Worked like a champ for me! Thank you!

  • Anonymous
    August 02, 2015
    Any powershell script to compare 2 document libraries on different site?

  • Anonymous
    January 22, 2016
    Great! Appreciate your help Thanks!

  • Anonymous
    February 25, 2016
    Great!! Thank you very much!! Saved lot of time..