Download all files from document libraries using PowerShell in SharePoint 2013 or SharePoint 2016

Download all the files from libraries using PowerShell

Objective:
In this article I would like to walk you through the process to download all the files in from all the document libraries in a SharePoint site collection.

Scenario:
It is always good to know the scenario before we learn something. The scenario for implementing this script is, in an organization finance team want to withdraw all the documents from SharePoint site because of various security and confidentiality reasons.
I have implemented the below script to download all the files from SharePoint libraries across the site collection.

Below is the PowerShell script to download files from sharepoint document library powershell:




param
(
[Parameter(Mandatory=$true)]
[ValidateScript({asnp *sh* -EA SilentlyContinue;if (Get-SPSite $_){$true}else{Throw “Site collection $_ does not exist”}})]
[string]$SiteCollection,
[Parameter(Mandatory=$true)]
[ValidateScript(
{
if (Test-Path $_)
{$true}
else{
$d = $_
$title = “Destination folder $_ doesn’t exist?”;
$message = “`n`n Do you want to create the folder through script ?”;
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”, “Creates directory $_”;
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”, “Exits script”;
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no);
$result = $host.ui.PromptForChoice($title,$message,$options,0);
switch($result)
{
0 {New-Item $d -Type Directory;$true}
1 {Throw “`nPlease create the destination folder before executing the script. `nTerminating the script !!!”}
}
}
})]
[string]$Destination
)

Asnp *sh* -EA SilentlyContinue
Start-SPAssignment -Global | Out-Null
function Get-SPWebs($SiteCollection)
{
$SiteCollection = Get-SPSite $SiteCollection
$webs = @()
$SiteCollection.allwebs | %{$webs += $_.url}
return $webs
}

function Get-SPFolders($webs)
{
foreach($web in $webs)
{
$web = Get-SPWeb $web
Write-Host “`n$($web.url)”
$lists = $web.lists | ?{$_.itemcount -ge “1” -And $_.Hidden -eq $false -And $_.BaseType -eq “DocumentLibrary”} #Excludes all hidden libraries and empty libraries
#$lists = $web.lists | ?{$_.title -eq “Documents” -and $_.itemcount -ge “1” -And $_.BaseType -eq “DocumentLibrary”} #Change any identifier here
foreach($list in $lists)
{
Write-Host “Library Name- $($list.RootFolder.url)”
#Download files in root folder
$rootfolder = $web.GetFolder($list.RootFolder.Url)
Download-SPContent $rootfolder -fldName 0

[string]$fld = $destination + “\” + $rootfolder.Name
if (Test-Path $fld)
{$true}
else{
New-Item $fld -Type Directory;$true
}

#Download files in subfolders
foreach($folder in $list.folders)
{
[string]$fld = $destination + “\” + $folder.url.Replace(“/”,”\”)
if (Test-Path $fld)
{$true}
else{
New-Item $fld -Type Directory;$true
}
$folder = $web.GetFolder($folder.url)
Download-SPContent $folder -fldName 1
}
}
$web.dispose()
}
}

function Download-SPContent($folder,$fldName)
{
if($fldName -eq “0”)
{
[string]$rtfld = $destination + “\” + $folder.Name
if (Test-Path $rtfld)
{$true}
else{
New-Item $rtfld -Type Directory;$true
}
}
foreach($file in $folder.Files)
{
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destination + “/” + $folder.url + “/” + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$stream.Close()
$writer.Close()
}
}

$webs = Get-SPWebs -SiteCollection $Sitecollection
Get-SPFolders -Webs $webs

Stop-SPAssignment -Global

The output will be as follows:



Download all the files from libraries using PowerShell
Download all the files from libraries using PowerShell

Advantages:
1. It will show all the newly created folders during this export
2. Replicates same structure on file system
3. It iterates through all the folders and replicates the files
4. You can restrict which folder you want to export
5. Easily customizable as per the user requirement with minimal technical knowledge


Limitations:
You can’t get the metadata associated with the files.

Hope this helps !!!


You May Also like the Following SharePoint Online Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →