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.
If you need to upload folder structure along with files, use: Upload Folder to SharePoint Online using PowerShell
Hello, very nice article. Can you upload, how to upload the contents in the folder in a similar technique ?