SharePoint 2010 | Download files from document library to network share using PowerShell



I had a request to download files with a certain name from a SharePoint 2010 document library. The problem was that there were 200k files in the library! I couldn't download them all, so I needed to selectively download them.

That's when I came across this PowerShell script

It was able to download the files, but I needed to filter out the files I didn't want. These are the modifications I made to the PowerShell Script:


This script takes as input the:
  1. $destination: Destination Location (Could be a network share drive)
  2. $webUrl: Url of the individual site
  3. $listUrl: Url of the list. Needs the full path, not the relative path.
  4. $searchterm: The term that we are searching for. Needs to have asterisks on either side of the term because those are wildcard characters.
Here is the modified script:

######################## Start Variables ########################
######################## Varun's Script######################
$destination = "C:\\temp\\download"
$webUrl = "https://sharepoint.contoso.com/sites/doccenter"
$listUrl = "https://sharepoint.contoso.com/sites/doccenter/Documents"
$searchterm = "*Important*"
##############################################################

$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)

function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
    if ($file.Name -like $searchterm) {
        #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)
}
Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.