Exporting SharePoint document using powershell

Few months back I found this PowerShell script and extended it to support a site collection.

$ver = $host | select version 
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} 
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
{ 
Add-PSSnapin Microsoft.SharePoint.PowerShell  -ErrorAction SilentlyContinue
} 


$Directory = "c:\ExportFiles\" 
 
## 
#Define Functions 
## 
 
Function TrimDirectory ($Directory) 
{ 
    if($Directory.EndsWith("\")) 
    { 
        Set-Variable -Name Directory -Value ($Directory.Substring(0, ($Directory.Length -1))) -Scope Script 
    } 
} 
 
Function EnsureDirectory ($Directory) 
{ 
    if(!(Test-Path $Directory)) 
    { 
        TrimDirectory $Directory 
                 
        New-Item -Path $Directory -ItemType Directory 
    } 
} 
 
Function ExportFiles ($SPList, $GalleryName) 
{ 
    EnsureDirectory ($Directory + "\" + $GalleryName) 
    Write-Host $SPlist.rootfolder.files.count
    foreach ($file in $SPlist.rootfolder.files) 
    { 
         
        $DestinationFile = ($Directory + "\$GalleryName\" + $file.name) 
        $FileBinary = $file.OpenBinary() 
        $FileStream = New-Object System.IO.FileStream($DestinationFile), Create 
        $Writer = New-Object System.IO.BinaryWriter($FileStream) 
        $Writer.write($FileBinary) 
        $Writer.close() 
    } 
     
    foreach($Folder in $SPList.Folders) 
    { 
         
        if($ParentFolderURL) 
        { 
            Remove-Variable ParentFolderURL 
        } 
     
        $i = 0 
     
        $folderURL = $Folder.url.split("/") 
     
        while($I -lt ($FolderURL.count -1)) 
        { 
            $ParentFolderURL = "$ParentFolderURL/" + $FolderURL[$I] 
            $I++ 
        } 
         
        $DownloadDirectory = ($Directory + "\$GalleryName\" + $Folder.url.substring($SPList.RootFolder.Url.Length)) -replace "/", "\" 
         
        EnsureDirectory $DownloadDirectory 
         
        foreach ($File in $Folder.Folder.Files) 
        { 
            $DestinationFile = ($Directory + "\$GalleryName\" + $Folder.url.Substring($SPList.Rootfolder.URL.Length) + "\" + $file.name) -replace "/", "\" 
             
            $FileBinary = $file.OpenBinary() 
 
            $FileStream = New-Object System.IO.FileStream($DestinationFile), Create 
 
            $Writer = New-Object System.IO.BinaryWriter($FileStream) 
 
            $Writer.write($FileBinary) 
 
            $Writer.close() 
        } 
         
         
    } 
} 
 
 
## 
#Start Script 
## 
 
 
TrimDirectory $Directory 
EnsureDirectory $Directory 

Start-SPAssignment -Global

$SiteURL = "http://intranet.hp.com"
$Site = Get-SPSite $SiteURL

foreach($Web in $Site.AllWebs)
{

    foreach($List in $Web.Lists)
    {
        if(!$List.Hidden)
        {
            $Title = $List.Title
            Write-Host $Title
            $ListFolder = New-Item -Path ($Directory + "\$Title") -ItemType Directory -ErrorAction SilentlyContinue
            ExportFiles $List $Title.ToString()
        }
    }
}

Stop-SPAssignment -Global

#New-Item -Path ($Directory + "\Documents") -ItemType Directory 
#$Documents = $Site.RootWeb.Lists | ? {$_.title -eq "Documents"} 
#ExportFiles $Documents "Documents" 
Advertisements

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