0

How to get folder structure of sharepoint online to local drive using recursive function in powershell. Can anyone please help me with the script. Thank you!

  • You need folder structure under specific list? – Deepmala Sep 18 '18 at 14:17
  • I need folder structure on local drive(eg: On Desktop) @Deepmala – BeginnerPower Sep 19 '18 at 6:20
0

The following PowerShell for your reference.

Function Get-SPOFolder
{
    <#
    .SYNOPSIS
        Get-SPOFolder is an advanced function which can be used to download folder from the libraray of SharePoint Online.
    .DESCRIPTION
        Get-SPOFolder is an advanced function which can be used to download folder from the libraray of SharePoint Online.
    .PARAMETER  downloadPath
        This parameter specifies the folder to be save the downloaded folder.
    .PARAMETER  userName
        Specifies the SharePoint Online user to be used to download file.
    .PARAMETER  siteURL
        This parameter specifies the SharePoint Online site to be used to download the files.
    .PARAMETER  ListName
        This parameter specifies the List/library to download the file.
    .PARAMETER  FolderName
        This parameter specifies the folder structure and name to be used to download the uploaded file.  
    .EXAMPLE
        C:\PS> Get-SPOFolder -downloadPath "C:\documents" -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document –FolderName PlanLib\PastPlan

        Download a folder PastPlan from the folder 'PlanLib' of library 'Document' of site 'https://tenant.sharepoint.com/sites/Plan'.
#>
 [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'Container'})] 
        [String]$downloadPath,
        [Parameter(Mandatory=$true)]
        [string] $userName,
        [Parameter(Mandatory=$True)]
        [Security.SecureString]$Password,
        [Parameter(Mandatory=$true)]
        [string] $siteURL,
        [Parameter(Mandatory=$true)]
        [string] $ListName,
        [Parameter(Mandatory=$true)]
        [string] $FolderName
    )


    Begin
    {
        $ErrorActionPreference = 'Stop'
        $SPClientDirRegKey = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\SharePoint Client Components\15.0" -PSProperty "Location" -ErrorAction:SilentlyContinue
        if ($SPClientDirRegKey -ne $null) 
        {
            $moduleFilePath1 = $SPClientDirRegKey.'Location' + 'ISAPI\Microsoft.SharePoint.Client.dll'
            $moduleFilePath2 = $SPClientDirRegKey.'Location' + 'ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
            Import-Module $moduleFilePath1
            Import-Module $moduleFilePath2
        }
        else 
        {
            $errorMsg = "Please install SharePoint Server 2013 Client Components SDK"
            throw $errorMsg
            Exit
        }
        $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $Password)
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
        $context.Credentials = $credentials
        $web = $context.Web
        $site = $context.Site
        $context.Load($web)
        $context.Load($site)
        try
        {
            $context.ExecuteQuery()
        }
        Catch
        {
            Write-Error $Error[0]
            Exit
        }
        Write-host "Conntect To Sharepoint Site $siteURL"
    }
    Process
    {
        $list = $context.Web.Lists.GetByTitle($ListName)
        $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        if($FolderName -ne "")
        {
            if($FolderName.StartsWith('/'))
            {
                $query.FolderServerRelativeUrl= $web.ServerRelativeUrl + "/" + $listname  + $FolderName;
#                Write-Host $query.FolderServerRelativeUrl
            }
            else
            {
                $query.FolderServerRelativeUrl= $web.ServerRelativeUrl + "/" + $listname  + $("/"+$FolderName);
#                Write-Host $query.FolderServerRelativeUrl
            }
        }
        $items = $list.GetItems($query)
        $context.Load($items) 
        $context.ExecuteQuery()
        $items.Count
        $ListPath = $web.ServerRelativeUrl + "/" + $ListName
        foreach ($item in $items)
        {    
            if($item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType ]::Folder) 
            {
 #               Write-Host $item["FileRef"]
                $FolderString = $item["FileRef"].Replace($ListPath,"")
                $Filepath = Split-Path $Foldername
                if($Filepath -ne "")
                {
                    $FolderString = $FolderString.Replace("/"+$Filepath,"")
                }

#                Write-Host "ListPath: $ListPath "
               $FolderString = $FolderString.Replace("/","\")
                Write-Host "FolderString is $FolderString"
                New-Item -ItemType directory -Path $($downloadPath+ "\" +$FolderString) -ErrorAction SilentlyContinue
            }


        }
    }
}
Get-SPOFolder -downloadPath "D:\DL" -userName lz@xxx.onmicrosoft.com -siteURL "https://xxx.sharepoint.com/sites/lz" -ListName DL -FolderName /

Reference: How to automatically upload/download files to/from Office 365 SharePoint Online

Or we can map the document library as a local drive, for example, O:, and then use the Copy-Item cmdlet to copy the entire folder structure recursively:

Copy-Item O:\ –destination '\\DL\Documents\migrated' -recurse -container

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.