Articles about European Sharepoint Hosting Service
SharePoint Hosting – Upload Nested Folder Structure And Files On A SP Online Library
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#Function to Copy Multiple Files with Folder structure to SharePoint Online Document Library Function Migrate - PnPFolderToSPO() { param( [Parameter(Mandatory = $true)][string] $SiteURL, [Parameter(Mandatory = $true)][string] $SourceFolderPath, [Parameter(Mandatory = $true)][string] $LibraryName, [Parameter(Mandatory = $true)][string] $LogFile) Try { Add - content $Logfile - value "`n---------------------- File Upload Script Started: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-------------------" #Connect to PnP Online Connect - PnPOnline - Url $SiteURL - UseWebLogin #Get the Target Folder to Upload $Web = Get - PnPWeb $List = Get - PnPList $LibraryName - Includes RootFolder $TargetFolder = $List.RootFolder $TargetFolderSiteRelativeURL = $TargetFolder.ServerRelativeURL.Replace($Web.ServerRelativeUrl, "") #Get All Items from the Source $Source = Get - ChildItem - Path $SourceFolderPath - Recurse $SourceItems = $Source | Select FullName, PSIsContainer, @ { Label = 'TargetItemURL'; Expression = { $_.FullName.Replace($SourceFolderPath, $TargetFolderSiteRelativeURL).Replace("\"," / ")}} Add - content $Logfile - value "Number of Items Found in the Source: $($SourceItems.Count)" #Upload Source Items from Fileshare to Target SharePoint Online document library $Counter = 1 $SourceItems | ForEach - Object { #Calculate Target Folder URL $TargetFolderURL = (Split - Path $_.TargetItemURL - Parent).Replace("\"," / ") $ItemName = Split - Path $_.FullName - leaf #Replace Invalid Characters $ItemName = [RegEx]::Replace($ItemName, "[{0}]" - f([RegEx]::Escape([String] '\"*:<>?/\|')), '_') #Display Progress bar $Status = "uploading '" + $ItemName + "' to " + $TargetFolderURL + " ($($Counter) of $($SourceItems.Count))" Write - Progress - Activity "Uploading ..." - Status $Status - PercentComplete(($Counter / $SourceItems.Count) * 100) If($_.PSIsContainer) { #Ensure Folder $Folder = Resolve - PnPFolder - SiteRelativePath($TargetFolderURL + "/" + $ItemName) Write - host "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL" Add - content $Logfile - value "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL" } Else { #Upload File $File = Add - PnPFile - Path $_.FullName - Folder $TargetFolderURL Write - host "Uploaded File '$($_.FullName)' to Folder $TargetFolderURL" Add - content $Logfile - value "Uploaded File '$($_.FullName)' to Folder $TargetFolderURL" } $Counter++ } } Catch { Write - host - f Red "Error:" $_.Exception.Message Add - content $Logfile - value "Error:$($_.Exception.Message)" } Finally { Add - content $Logfile - value "---------------------- File upload Script Completed: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-----------------" } } #Call the Function to Upload a Folder to SharePoint Online 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!
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.
Print article | This entry was posted by Peter on August 31, 2020 at 5:09 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |