Upload Nested Folder Structure And Files On A SP Online Library

Introduction 

 
Hi guys, let's explore some amazing ways to completely migrate a nested folder structure with lots of different files which is from your local path to a SP online library using PnP PowerShell.
 
Here is the script to run on Windows ISE Powershell:
  1. #Function to Copy Multiple Files with Folder structure to SharePoint Online Document Library  
  2. Function Migrate - PnPFolderToSPO() {  
  3.         param(  
  4.             [Parameter(Mandatory = $true)][string] $SiteURL,  
  5.             [Parameter(Mandatory = $true)][string] $SourceFolderPath,  
  6.             [Parameter(Mandatory = $true)][string] $LibraryName,  
  7.             [Parameter(Mandatory = $true)][string] $LogFile)  
  8.         Try {  
  9.             Add - content $Logfile - value "`n---------------------- File Upload Script Started: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-------------------"  
  10.             #Connect to PnP Online  
  11.             Connect - PnPOnline - Url $SiteURL - UseWebLogin  
  12.             #Get the Target Folder to Upload  
  13.             $Web = Get - PnPWeb  
  14.             $List = Get - PnPList $LibraryName - Includes RootFolder  
  15.             $TargetFolder = $List.RootFolder  
  16.             $TargetFolderSiteRelativeURL = $TargetFolder.ServerRelativeURL.Replace($Web.ServerRelativeUrl, "")  
  17.             #Get All Items from the Source  
  18.             $Source = Get - ChildItem - Path $SourceFolderPath - Recurse  
  19.             $SourceItems = $Source | Select FullName, PSIsContainer, @ {  
  20.                     Label = 'TargetItemURL';  
  21.                     Expression = {  
  22.                             $_.FullName.Replace($SourceFolderPath, $TargetFolderSiteRelativeURL).Replace("\"," / ")}}  
  23.                                 Add - content $Logfile - value "Number of Items Found in the Source: $($SourceItems.Count)"  
  24.                                 #Upload Source Items from Fileshare to Target SharePoint Online document library $Counter = 1 $SourceItems | ForEach - Object {  
  25.                                     #Calculate Target Folder URL  
  26.                                     $TargetFolderURL = (Split - Path $_.TargetItemURL - Parent).Replace("\"," / ")  
  27.                                         $ItemName = Split - Path $_.FullName - leaf #Replace Invalid Characters $ItemName = [RegEx]::Replace($ItemName, "[{0}]" - f([RegEx]::Escape([String]  
  28.                                             '\"*:<>?/\|')), '_') #Display Progress bar $Status = "uploading '" + $ItemName + "' to " + $TargetFolderURL + " ($($Counter) of $($SourceItems.Count))"  
  29.                                         Write - Progress - Activity "Uploading ..." - Status $Status - PercentComplete(($Counter / $SourceItems.Count) * 100) If($_.PSIsContainer) {  
  30.                                             #Ensure Folder  
  31.                                             $Folder = Resolve - PnPFolder - SiteRelativePath($TargetFolderURL + "/" + $ItemName)  
  32.                                             Write - host "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL"  
  33.                                             Add - content $Logfile - value "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL"  
  34.                                         }  
  35.                                         Else {  
  36.                                             #Upload File  
  37.                                             $File = Add - PnPFile - Path $_.FullName - Folder $TargetFolderURL  
  38.                                             Write - host "Uploaded File '$($_.FullName)' to Folder $TargetFolderURL"  
  39.                                             Add - content $Logfile - value "Uploaded File '$($_.FullName)' to Folder $TargetFolderURL"  
  40.                                         }  
  41.                                         $Counter++  
  42.                                     }  
  43.                                 }  
  44.                                 Catch {  
  45.                                     Write - host - f Red "Error:"  
  46.                                     $_.Exception.Message  
  47.                                     Add - content $Logfile - value "Error:$($_.Exception.Message)"  
  48.                                 }  
  49.                                 Finally {  
  50.                                     Add - content $Logfile - value "---------------------- File upload Script Completed: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-----------------"  
  51.                                 }  
  52.                             }  
  53.                             #Call the Function to Upload a Folder to SharePoint Online  
  54.                             Migrate - PnPFolderToSPO - SiteURL "https://samplesharenet.sharepoint.com/sites/classictest" - SourceFolderPath "C:\Users\Bharat\Documents\Bharat\General Works for Internal Requirements" - LibraryName "PnPCopytoLib" - LogFile "C:\Users\Bharat\PnPTest\Migration-LOG.log"  
Just enter your own respective details which are highlighted in the above script.
 
When it starts running, enter your Site Admin/Global Admin Tenant details to start the processing.
 
You can see the Uploading in Progress on your Windows Powershell ISE Interface. Just wait until it gets finished!
 
Note
This script copies the contents of a folder to the SharePoint Online document library. It overwrites any existing file in the target SharePoint Online library. It creates a folder if it doesn't exist on the target site already. We can also use this script to migrate files and folders from network file share to SharePoint Online document library!
 
Creative Idea
 
You can have the above script triggered through a Flow/Azure Flow[Logic Apps] to have an automated Copying and include Removal in the Source to establish an Automated Archival Process for your Business needs.
 
Cheers!