Download files from SharePoint document library using PowerShell

This PowerShell SharePoint tutorial, we will discuss how to download all the files from all the document libraries in a SharePoint site collection. The same PowerShell script, we can use to download files from SharePoint 2013/2016 document library.

It is always good to know the scenario before we learn something. The scenario for implementing this script is, in an organization finance team wants to withdraw all the documents from the SharePoint site because of various security and confidentiality reasons.

Download files from sharepoint document library PowerShell

I have implemented the below PowerShell script to download all the files from SharePoint libraries across the site collection. You can run the PowerShell script using PowerShell ISE.

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 files from sharepoint document library powershell

Advantages of this PowerShell Script

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

Limitations of this PowerShell script

  • You can’t get the metadata associated with the files.

You may like following PowerShell tutorials:

This PowerShell tutorial, we discussed how to download files from SharePoint document library using PowerShell in SharePoint 2013/2016

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ 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

>