Hi,
I’ve been working on a script that will allow you to download all files that are stored in SharePoint in a given site collection.
If the path does not exist, the script will prompt you to create it for you. Before the script runs, it also checks if the site collection exists.
Run the script like this:
.\Get-SPContent.ps1 -SiteCollection "<SiteCollectionURL>" -Destination "<Path>"
The console shows which libraries were exported to your file system.
—– * Advanced * —–
If you have specific requirements as to which (type of) libraries you want to export, you can change the following line to fit your requirements:
$lists = $web.lists | ?{$_.itemcount -ge "1" -And $_.Hidden -eq $false -And $_.BaseType -eq "DocumentLibrary"} #Excludes all hidden libraries and empty libraries
Below is the code you can save as Get-SPContent.ps1
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 = "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); switch($result) { 0 {New-Item $d -Type Directory;$true} 1 {Throw "Please create the folder before running the script again. `nExiting 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 "- $($list.RootFolder.url)" #Download files in root folder $rootfolder = $web.GetFolder($list.RootFolder.Url) Download-SPContent($rootfolder) #Download files in subfolders foreach($folder in $list.folders) { $folder = $web.GetFolder($folder.url) Download-SPContent($folder) } } $web.dispose() } } function Download-SPContent($folder) { foreach($file in $folder.Files) { $binary = $file.OpenBinary() $stream = New-Object System.IO.FileStream($destination + "/" + $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
