SharePoint Online: Upload Folder Structure using PowerShell

Requirement: Upload folder structure to SharePoint Online using PowerShell.

PowerShell to Upload Folder Structure to SharePoint Online:

I had to clone a folder structure from a local drive to SharePoint Online. Uploading a folder structure to SharePoint Online can be a tedious process, especially if you have a lot of files and folders to upload.

Uploading a folder tree through the web browser or File explorer also uploads all files within those folders. So, Here is the PowerShell to upload a folder structure from the local drive to the SharePoint Online document library without copying any files:

#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 to Check if Folder Exists. If not, Create the Folder
Function Ensure-SPOFolder()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $FolderRelativeURL
    )
 
    #Check Folder Exists
    Try {
        $Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)
        $Ctx.Load($Folder)
        $Ctx.ExecuteQuery() 
  
        Write-host -f Yellow "`tFolder Already Exists! Skipped...!"
    }
    Catch {
        #Create New Sub-Folder
        $Folder=$Web.Folders.Add($FolderRelativeURL)
        $Ctx.ExecuteQuery()
        Write-host -f Green "`tCreated Folder at "$FolderRelativeURL
    }
}
  
#Function to upload a Local Folder structure to SharePoint Online Documnet Library
Function Upload-SPOFolderStructure()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $SourceFolderPath
    )

    #Setup Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
       
    #Get the Target Folder to Upload
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $TargetFolder = $List.RootFolder
    $Ctx.Load($TargetFolder)
    $Ctx.ExecuteQuery()
 
    #Get All Folders and Sub-Folders from the Local Source
    Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
        If($_.PSIsContainer -eq $True)
        {
            $FolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
            If($FolderRelativeURL) 
            {
                Write-host -f Yellow "Ensuring Folder '$FolderRelativeURL' Exists..."
                #Call function to ensure folder
                Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL
            }
        }
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
$SourceFolderPath="C:\Users\salaudeen\Desktop\Upload"
  
#Call the function to Upload All folders and sub-folders from local folder to SharePoint Online library
Upload-SPOFolderStructure -SiteURL $SiteURL -ListName $ListName -SourceFolderPath $SourceFolderPath

This script copies all folders and sub-folders to the SharePoint Online document library from the given source.

SharePoint Online Upload Folder Structure using PowerShell

If you need to upload folder structure along with files, use: Upload Folder to SharePoint Online using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Upload Folder Structure using PowerShell

  • I have this error:
    Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the Microsoft account system.”
    At line:24 char:9
    + $Ctx.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdcrlException

    the user I’m testing does not have active MFA.

    Reply
  • Hello, very nice article. Can you upload, how to upload the contents in the folder in a similar technique ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *