Download all Site collection documents / Files using Powershell in specific folder

I have updated the script written by “Nico Martens” in below blog post

Below script will download all your documents in specific folder of Site and List

Save this script as Get-SPContent.ps1 and run using below line

.\Get-SPContent.ps1 -SiteCollection "" -Destination ""

[ValidateScript({asnp *sh* -EA SilentlyContinue;if (Get-SPSite $_){$true}else{Throw “Site collection $_ does not exist”}})]
if (Test-Path $_)
$d = $_
$title = “Create Folder?”;
$message = “$_ doesn’t exist, do you want the script to create it?”;
$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,1);
0 {New-Item $d -Type Directory;$true}
1 {Throw “Please create the folder before running the script again. `nExiting script”}
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 “- $($list.RootFolder.url)”
$DirpathList = $Dirpath +”\”+$list.title
#Download files in root folder
$rootfolder = $web.GetFolder($list.RootFolder.Url)

#Download files in subfolders
foreach($folder in $list.folders)
$folder = $web.GetFolder($folder.url)



function CreateFolder($Dirpath)
#Write-Host $Dirpath
New-Item -ItemType directory -Path $Dirpath


function Download-SPContent($folder)
foreach($file in $folder.Files)
$binary = $file.OpenBinary();

$stream = New-Object System.IO.FileStream($DirpathList+ “/” + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)

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

Stop-SPAssignment -Global






Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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