Forum Discussion
Download folder or library from sharepoint online using powershell
Hi,
I'm looking for a way to download a folder or library from SharePoint Online by using Powershell.
At the moment I'm able to upload or download a file + upload a folder to SPO by using PnP.
Regards
- There are a few ways to go about this, but the easiest is to use WebDav for the path and then basic file commands such as Get-ChildItem, will just work. The general format is: \\yoursite.sharepoint.com@SSL\DavWWWRoot\ServerRelativePath
- DougWareBrass ContributorThere are a few ways to go about this, but the easiest is to use WebDav for the path and then basic file commands such as Get-ChildItem, will just work. The general format is: \\yoursite.sharepoint.com@SSL\DavWWWRoot\ServerRelativePath
- Bernd VerhofstadtIron Contributor
Thanks for providing this idea, I was able to download a folder in only few lines!
$webDavUrl = "\\yoursite.sharepoint.com@SSL\DavWWWRoot\ServerRelativePath" Get-ChildItem -Path $webDavUrl -Recurse | % { $dest = Join-Path -Path "\\domain\folder" -ChildPath (Split-Path $_.FullName -NoQualifier).Replace($webDavUrl, ''); Copy-Item $_.FullName -Destination $dest -Force }
- In both cases you will need to iterate throught the content of the folder
- John_McClaneCopper Contributor
I know that is an old topic, but I solved my issue with this Script found in this link https://www.sharepointdiary.com/2016/09/sharepoint-online-download-file-from-library-using-powershell.html and adapted for me. Just change the variables in BOLD:
--------------------------------------------------------------------------------------------------
Function Download-SPOFolder()
{
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 fileS
$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-SPOFolder -SiteURL $SiteURL -SourceFolder $Folder -TargetFolder $TargetFolder
}
}
}
Catch {
write-host -f Red "Error Downloading Folder!" $_.Exception.Message
}
}
#Set parameter values
$orgName="myorganizationname"
$admin="admin@mydomain.com"
$SiteURL="https://myorganizationname.sharepoint.com/sites/sitename"
$FolderRelativeUrl="/Shared%20Documents"
$TargetFolder="C:\foldername"
#Setup Credentials to connect
$cred = Get-Credential -UserName $admin -Message GlobalAdminLogin
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $cred
$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 Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
$Web.ServerRelativeUrl+$FolderRelativeUrl
#Get the Folder
$SourceFolder = $Web.GetFolderByServerRelativeUrl($Web.ServerRelativeUrl+$FolderRelativeUrl)
$Ctx.Load($SourceFolder)
$Ctx.ExecuteQuery()
#Call the function to download Folder
Download-SPOFolder -SiteURL $SiteURL -SourceFolder $SourceFolder -TargetFolder $TargetFolder
--------------------------------------------------------------------------------------------------
and it works with OneDrive too, by changing the following variables:
$SiteURL="https://myorganizationname-my.sharepoint.com/personal/user_mydomain_com"
$FolderRelativeUrl="/Documents"