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

  1. Justin says:

    Very cool. I hope all of this is ending up in the Technet gallary!

  2. I was looking for this for long, thanks.

  3. yo says:

    this does copy and paste. It paste as one long line

  4. alan-p says:

    does it copy versions of a document also?

  5. Milton Lopez says:

    The code spills out of the right marging for some lines …

  6. Meera says:

    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

  7. jouluq says:

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

  8. Umar says:

    Any body tried this for SP 2013 ? any different ?

  9. Roby says:

    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?

  10. Gogu says:

    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

  11. Prakash says:

    how do we delete a document from SharePoint after it is exported?

  12. Shola Salako says:

    Exactly what we needed!  Thanks for sharing this.  Cheers!

  13. Amar Hegde says:

    Excellent Varun!!.. Helped me a lot….

  14. Veena says:

    How can permissions be applied as ACLs?

  15. Robert says:

    Worked like a champ for me! Thank you!

  16. Ashish says:

    Any powershell script to compare 2 document libraries on different site?

  17. Esteban says:

    Great!

    Appreciate your help

    Thanks!

  18. Madhav says:

    Great!!

    Thank you very much!! Saved lot of time..

Skip to main content