SharePoint – Export All Documents on Site and Sub-Sites

 

I was eliminating a site on SharePoint that had over 50 sub-sites. I wanted an easy way to get all the documents in each site without having to manually visit each one. Here is an easy PowerShell script I came up with.

   1: #Created by Brandon Claps

   2: #9/25/14

   3: #Exports all documents under a specific site and sub-sites to a local directory

   4:  

   5: #Address of your SharePoint root site

   6: $MainSiteAddress = "https://sharepointsite.contoso.com"

   7:  

   8: #URL of what you you would like to gather. Type a single asterisk if you want everything

   9: $MatchURL = "*URLAddress*"

  10:  

  11: #Destination where you want to download the documents to

  12: $destination = "D:\Exported Documents"

  13:  

  14:  

  15: $Site = Get-SPSite $MainSiteAddress

  16: $WebCollection = $Site.AllWebs

  17:  

  18: foreach($Web in $WebCollection)

  19: {

  20:     foreach($List in $Web.Lists)

  21:     {

  22:         if($List.ParentWebUrl -like $MatchURL)

  23:         {

  24:             if($List.BaseTemplate -eq "DocumentLibrary")

  25:             {                                

  26:                 $webUrl = $MainSiteAddress + $List.ParentWebUrl

  27:                 $listUrl = $webUrl + "/" + $List.RootFolder.Url

  28:                 

  29:                 $web2 = Get-SPWeb -Identity $webUrl

  30:                 $list2 = $web2.GetList($listUrl)

  31:                 

  32:                 function ProcessFolder 

  33:                 {

  34:                     param($folderUrl)

  35:                     $folder = $web2.GetFolder($folderUrl)

  36:                     

  37:                     foreach ($file in $folder.Files) 

  38:                     {

  39:                         $destinationfolder = $destination + "/" + $List.ParentWebUrl.TrimStart("/").Replace("/","-") + "---" + $folder.Url 

  40:                         if (!(Test-Path -path $destinationfolder))

  41:                         {

  42:                             $dest = New-Item $destinationfolder -type directory 

  43:                         }

  44:                         

  45:                         $binary = $file.OpenBinary()

  46:                         $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create

  47:                         $writer = New-Object System.IO.BinaryWriter($stream)

  48:                         $writer.write($binary)

  49:                         $writer.Close()

  50:                     }

  51:                 }

  52:                 Write-Host "Downloading: " $listUrl 

  53:                 ProcessFolder($list2.RootFolder.Url)

  54:             

  55:                 foreach ($folder in $list2.Folders) {

  56:                     ProcessFolder($folder.Url)

  57:                 }                                

  58:             }        

  59:         }        

  60:     }

  61:  }

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>