GitHub Icon Image

Upload local files and folders to SharePoint Online

Summary

This script shows how you can use the CLI to upload files located on a local folder to a SharePoint Online library or subfolder. This is a simple script that could be used for simple data migration scenarios. The given example uploads to the given site to Shared Documents library all files and sub folders of ./import local folder

  • CLI for Microsoft 365 with PowerShell
Write-host 'upload files and folders from directory example'

function Import-FilesAndFolders(
  [Parameter(Mandatory = $True)][string] $folderPath,
  [Parameter(Mandatory = $True)][string] $sPFolderPath,
  [Parameter(Mandatory = $True)][string] $siteUrl) {
    $items = Get-ChildItem -Path $folderPath
    foreach ($item in $items) {
        if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) {
          Write-host "creating folder $item"
          $folderCreated = m365 spo folder add --webUrl $siteUrl --parentFolderUrl $sPFolderPath --name $item.Name

          Write-host "importing folder $item"
          Import-FilesAndFolders  -folderPath $item.FullName -sPFolderPath "$sPFolderPath/$item" -siteUrl $siteUrl
        }
        else {
          Write-host "importing file $item"

          m365 spo file add --webUrl $siteUrl --folder $sPFolderPath --path $item.FullName
        }
    }
}

Write-host 'ensure logged in'
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

$importFolderPath = './import'
$sPFolderPath = '/Shared Documents'
$siteUrl = 'https://contoso.sharepoint.com/sites/TestFileImport'
Import-FilesAndFolders -folderPath $importFolderPath -sPFolderPath $sPFolderPath -siteUrl $siteUrl

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Source Credit

Sample first appeared on Upload local files and folders to SharePoint Online | CLI for Microsoft 365

Contributors

Author(s)
Patrick Lamber
Adam Wójcik

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI