Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Load SharePoint CSOM Assemblies
- Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Function Download-AllFilesFromLibrary()
- {
- param
- (
- [Parameter(Mandatory=$true)] [string] $SiteURL,
- [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
- [Parameter(Mandatory=$true)] [string] $TargetFolder
- )
- Try {
- #Create Local Folder, if it doesn't exist
- $FolderName = ($SourceFolder.ServerRelativeURL) -replace "/","\"
- $LocalFolder = $TargetFolder + $FolderName
- If (!(Test-Path -Path $LocalFolder)) {
- New-Item -ItemType Directory -Path $LocalFolder | Out-Null
- }
- #Get all Files from the folder
- $FilesColl = $SourceFolder.Files
- $Ctx.Load($FilesColl)
- $Ctx.ExecuteQuery()
- #Iterate through each file and download
- Foreach($File in $FilesColl)
- {
- $TargetFile = $LocalFolder+"\"+$File.Name
- #Download the file
- $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$File.ServerRelativeURL)
- $WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)
- $FileInfo.Stream.CopyTo($WriteStream)
- $WriteStream.Close()
- write-host -f Green "Downloaded File:"$TargetFile
- }
- #Process Sub Folders
- $SubFolders = $SourceFolder.Folders
- $Ctx.Load($SubFolders)
- $Ctx.ExecuteQuery()
- Foreach($Folder in $SubFolders)
- {
- If($Folder.Name -ne "Forms")
- {
- #Call the function recursively
- Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $Folder -TargetFolder $TargetFolder
- }
- }
- }
- Catch {
- write-host -f Red "Error Downloading Files from Library!" $_.Exception.Message
- }
- }
- #Set parameter values
- $SiteURL="https://crescent.sharepoint.com/sites/sales/"
- $LibraryName="Documents"
- $TargetFolder="C:\Docs"
- #Setup Credentials to connect
- $Cred= Get-Credential
- $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
- #Setup the context
- $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
- $Ctx.Credentials = $Credentials
- #Get the Library
- $List = $Ctx.Web.Lists.GetByTitle($LibraryName)
- $Ctx.Load($List)
- $Ctx.Load($List.RootFolder)
- $Ctx.ExecuteQuery()
- #Call the function to download file
- Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $List.RootFolder -TargetFolder $TargetFolder
- #Read more: http://www.sharepointdiary.com/2017/03/sharepoint-online-download-all-files-from-document-library-using-powershell.html#ixzz5u3iP1Hx1
Add Comment
Please, Sign In to add comment