Introduction

 Below provided PowerShell will come very handy if we need to upload multiple files to SharePoint Online document Library.

In this script, we iterate through local folder provided and upload files to specified SharePoint Online document library. It will keep the same folder structure in SharePoint document library as created in the local Folder system.

Pre Requisite

Install latest SharePointPnPPowerShellOnline.msi from location https://github.com/SharePoint/PnP-PowerShell/releases Jump

Process

Overall process consists of three steps: First step is to connect to SharePoint Online Site Collection

$credentials = Get-Credential
Connect-PnPOnline -Url $siteUrl -CreateDrive -Credentials $credentials 

$siteUrl : This is the location of source SharePoint Online site.

The second step is to iterate through the folder location and access all file / folder name

  (dir $path -Recurse) | %{
   try{
      if($_.GetType().Name -eq "FileInfo"){
         $SPFolderName =  $documentLibraryName + $_.DirectoryName.Substring($path.Length);
        . . . . 

$documentLibraryName : Name of the Document Library in SharePoint site where we need to upload documents
$path: Local folder path

Finally, we use "Add-PnPFile" command of PnP SharePoint Online PowerShell to upload files. Overall script will look as follows:

$file = Get-ChildItem -Path $LocalFolderLocation -Recurse
$i = 0;
Write-Host "Uploading documents to Site.." -ForegroundColor Cyan 
(dir $path -Recurse) | %{
  try
      $i++
if($_.GetType().Name -eq "FileInfo"){
   $SPFolderName =  $documentLibraryName + $_.DirectoryName.Substring($path.Length);
   $status = "Uploading Files :'" + $_.Name + "' to Location :" + $SPFolderName
    Write-Progress -activity "Uploading Documents.." -status $status -PercentComplete (($i / $file.length)  * 100)
$te = Add-PnPFile -Path $_.FullName -Folder $SPFolderName
}        
    }
catch{ }
}

 

We can also use command like -Values @{Modified="1/1/2016"} to add specific column values. Check reference for more variations.

References