Skip to content

Instantly share code, notes, and snippets.

@cloaked-ninja
Created October 13, 2016 22:30
Show Gist options
  • Save cloaked-ninja/9ff2991353238270446af208f039aa52 to your computer and use it in GitHub Desktop.
Save cloaked-ninja/9ff2991353238270446af208f039aa52 to your computer and use it in GitHub Desktop.
download files from sharepoint
$destination = "C:\users\userName\desktop\testFolder"
$webUrl = "http://sharetest"
$listUrl = "http://sharetest/office"
##############################################################
$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)
function ProcessFolder {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
#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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment