SOLVED

How to download root level files from OneDrive using PowerShell?

%3CLINGO-SUB%20id%3D%22lingo-sub-758814%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-758814%22%20slang%3D%22en-US%22%3EThere%20are%20for%20sure%20a%20lot%20of%20examples%20over%20the%20Internet%20that%20you%20could%20easily%20adapta...for%20instance%3A%20%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2Fdocuments-from-SharePoint-99e58875%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2Fdocuments-from-SharePoint-99e58875%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-760122%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-760122%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Avian%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUse%20following%20code%2C%20hope%20this%20helps%3C%2FP%3E%3CP%3E%3D%3D%3D%3C%2FP%3E%3CP%3E%23Module%20name%3A%20FinalDownload.ps1%3CBR%20%2F%3E%23Modified%20By%3A%20Ashish%20Kanoongo%26nbsp%3B%3CBR%20%2F%3E%23Date%3A%2007-18-2017(mm%2Fdd%2Fyyyy)%3CBR%20%2F%3E%23Purpose%3A%20Download%20Files%20or%20folders%20from%20OneDrive%20Site%3C%2FP%3E%3CP%3E%23Download%20SharePointPnp%20PowerShell%20Module%20from%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%3C%2FA%3E%3CBR%20%2F%3E%24siteUrl%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Ftenantname-my.sharepoint.com%2Fpersonal%2Fusername_domain_com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftenantname-my.sharepoint.com%2Fpersonal%2Fusername_domain_com%3C%2FA%3E%22%3CBR%20%2F%3E%24downloadLocation%20%3D%20'C%3A%5Ctemp'%3C%2FP%3E%3CP%3E%23If%20MFA%20is%20not%20implement%20then%20use%20following%3CBR%20%2F%3E%23%24cred%20%3D%20Get-Credential%20-UserName%20%24userName%20-Message%20%22Enter%20Password%22%3CBR%20%2F%3E%23Connect-PnPOnline%20-Url%20%24siteUrl%20-CreateDrive%20-Credentials%20%24cred%3C%2FP%3E%3CP%3E%23If%20MFA%20is%20Implemented%2C%20thne%20use%20following%3CBR%20%2F%3EConnect-PnPOnline%20-Url%20%24siteUrl%20-UseWebLogin%20-CreateDrive%3C%2FP%3E%3CP%3E%23Return%20the%20current%20context%3CBR%20%2F%3E%24ctx%20%3D%20Get-PnPContext%3C%2FP%3E%3CP%3E%23Set%20the%20Library%2FList%20Name%20which%20will%20be%20used%20for%20download%3CBR%20%2F%3E%24list%20%3D%20Get-PnPList%20%22Documents%22%3C%2FP%3E%3CP%3E%23Use%20if%20you%20want%20to%20download%20rootlevel%20folders%3CBR%20%2F%3E%23%24files%20%3D%20%24list.RootFolder.Folders%3C%2FP%3E%3CP%3E%23Use%20if%20you%20want%20to%20download%20rootlevel%20files%3CBR%20%2F%3E%24files%20%3D%20%24list.RootFolder.Files%3C%2FP%3E%3CP%3E%23Load%20the%20files%20or%20folder%20of%20current%20context%20of%20select%20List%20or%20Library%3CBR%20%2F%3E%24ctx.Load(%24files)%3C%2FP%3E%3CP%3E%23Execute%20the%20Context%20Query%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3C%2FP%3E%3CP%3E%23Loop%20for%20Each%20item%20which%20needs%20to%20Download%3CBR%20%2F%3Eforeach%20(%24file%20in%20%24files)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23Retrieve%20each%20file%20and%20downloads%20it%20to%20the%20download%20folder%20location%3CBR%20%2F%3EGet-PNPFile%20-ServerRelativeUrl%20%24file.ServerRelativeUrl%20-Path%20%24DownloadLocation%20-Filename%20%24file.Name%20-AsFile%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-764061%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-764061%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F34656%22%20target%3D%22_blank%22%3E%40Ashish%20Kanoongo%3C%2FA%3E%26nbsp%3B%20Thanks%20for%20code.%20It%20saves%20my%20day.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EAvian%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-764062%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-764062%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Ashish%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20code.%20It%20saves%20my%20day.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EAvian%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-758689%22%20slang%3D%22en-US%22%3EHow%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-758689%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20have%20some%20one%20drive%20sites%20(approx%20100)%2C%20we%20want%20to%20download%20the%20root%20level%20files%20using%20CSOM%20and%20PowerShell.%20Can%20anyone%20please%20guide%26nbsp%3B%20on%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EAvian%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-758689%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOneDrive%20for%20Business%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1456860%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1456860%22%20slang%3D%22en-US%22%3E%3CP%3EAmazing%20this%20works%20so%20well%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1483495%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20download%20root%20level%20files%20from%20OneDrive%20using%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1483495%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F34656%22%20target%3D%22_blank%22%3E%40Ashish%20Kanoongo%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFurther%20Question%3A%26nbsp%3B%20how%20do%20i%20download%20from%20a%20%3CSTRONG%3Esub%3C%2FSTRONG%3Efolder%20%3F%3C%2FP%3E%3CP%3EExample%20subfolder%3A%20Documents%2FGeneral%2FEmail%20Messages%2F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3EMarcelo%20Finkielsztein%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

Hello,

 

We have some one drive sites (approx 100), we want to download the root level files using CSOM and PowerShell. Can anyone please guide  on this?

 

Thanks

Avian

6 Replies
Highlighted
There are for sure a lot of examples over the Internet that you could easily adapta...for instance: https://gallery.technet.microsoft.com/office/documents-from-SharePoint-99e58875
Highlighted
Best Response confirmed by Avian 1 (Frequent Contributor)
Solution

Hi Avian,

 

Use following code, hope this helps

===

#Module name: FinalDownload.ps1
#Modified By: Ashish Kanoongo 
#Date: 07-18-2017(mm/dd/yyyy)
#Purpose: Download Files or folders from OneDrive Site

#Download SharePointPnp PowerShell Module from https://github.com/SharePoint/PnP-PowerShell
$siteUrl = "https://tenantname-my.sharepoint.com/personal/username_domain_com"
$downloadLocation = 'C:\temp'

#If MFA is not implement then use following
#$cred = Get-Credential -UserName $userName -Message "Enter Password"
#Connect-PnPOnline -Url $siteUrl -CreateDrive -Credentials $cred

#If MFA is Implemented, thne use following
Connect-PnPOnline -Url $siteUrl -UseWebLogin -CreateDrive

#Return the current context
$ctx = Get-PnPContext

#Set the Library/List Name which will be used for download
$list = Get-PnPList "Documents"

#Use if you want to download rootlevel folders
#$files = $list.RootFolder.Folders

#Use if you want to download rootlevel files
$files = $list.RootFolder.Files

#Load the files or folder of current context of select List or Library
$ctx.Load($files)

#Execute the Context Query
$ctx.ExecuteQuery()

#Loop for Each item which needs to Download
foreach ($file in $files)
{
#Retrieve each file and downloads it to the download folder location
Get-PNPFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $DownloadLocation -Filename $file.Name -AsFile
}

Highlighted

@Ashish Kanoongo  Thanks for code. It saves my day.

 

Regards

Avian

Highlighted

Hi Ashish,

 

Thanks for code. It saves my day.

 

Regards

Avian

Highlighted

Amazing this works so well

Highlighted

@Ashish Kanoongo 

 

Further Question:  how do i download from a subfolder ?

Example subfolder: Documents/General/Email Messages/

 

Thanks.

Marcelo Finkielsztein