Forum Discussion

Avian 1's avatar
Avian 1
Iron Contributor
Jul 17, 2019
Solved

How to download root level files from OneDrive using PowerShell?

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

  • 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
    }

7 Replies

  • Ashish Kanoongo's avatar
    Ashish Kanoongo
    Copper Contributor

    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
    }

    • MarceloFinki's avatar
      MarceloFinki
      Copper Contributor

      Ashish Kanoongo 

       

      Further Question:  how do i download from a subfolder ?

      Example subfolder: Documents/General/Email Messages/

       

      Thanks.

      Marcelo Finkielsztein

      • TassieTrooper's avatar
        TassieTrooper
        Copper Contributor

        MarceloFinki  In case you don't have the answer, this worked for me:

         

        #Download SharePointPnp PowerShell Module from https://github.com/SharePoint/PnP-PowerShell
        $FolderSiteRelativeUrl = "Documents/MySubFolder"
        $downloadLocation = 'C:\temp\MySiteSubfolder'

        #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

        #Use if you want to download rootlevel files
        $files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl

        #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
        }

         

         

    • Avian 1's avatar
      Avian 1
      Iron Contributor

      Hi Ashish,

       

      Thanks for code. It saves my day.

       

      Regards

      Avian

Resources