How to upload/download files to/from Office 365 SharePoint Online

Introduction

This script can upload/download files or folders to/from library of SharePoint Online

Prerequisites
This script requires SharePoint Server 2013 Client Components SDK. Please download and install the package from Microsoft Download Center. This script cannot work correctly without this package.  

Scenarios

This script can upload/download files or folders to/from library of SharePoint Online

Script

This script contains the following advanced functions:

You can use this script in the following way.

1. Open Windows PowerShell.

2. Run Import-Module cmdlet to import this module file.
Import-Module <ModulePath>\SPOUploadDownload

Here are some code snippets for your reference.

PowerShell
Edit|Remove
Write-Host "Creating Folders" 
           if($item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType ]::Folder)  
           { 
               Write-Host $item["FileRef"] 
               $FolderString = $item["FileRef"].Replace($ListPath,"") 
               Write-Host "ListPath: $ListPath " 
               $FolderString = $FolderString.Replace("/","\") 
               Write-Host "$FolderString" 
               New-Item -ItemType directory -Path $downloadPath$FolderString -ErrorAction SilentlyContinue 
 
 

Examples

Example 1: Upload a file named ‘Plan.docx’ to the folder ‘PlanLib’ of library ‘Document’ of site ‘https://tenant.sharepoint.com/sites/Plan’ .
 
Add-SPOFile -UploadFilePath "C:\documents\plan.docx " -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document -FolderPath PlanLib
Command Screenshot:
 

Result Screenshot: 

 

 

Example 2: Upload a list of files to the folder ‘Sep’ of library ‘Document’ of site ‘https://tenant.sharepoint.com/sites/Plan’.

$files | Add-SPOFile -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document -FolderPath Sep
Command Screenshot:


Result Screenshot:


Example 3: Upload a folder PastPlan to the folder ‘PlanLib’ of library ‘Document’ of site ‘https://tenant.sharepoint.com/sites/Plan’ .

Add-SPOFolder -UploadfolderPath "C:\documents\PastPlan" -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document -FolderPath PlanLib
Command Screenshot:

Result Screenshot:


Example 4: Download a file ‘Plan.docx’ from the folder ‘PlanLib’ of library ‘Document’ of site ‘https://tenant.sharepoint.com/sites/Plan’ . 

Get-SPOFile  - downloadPath "C:\documents " -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document –FolderName PlanLib  -FileName Plan.docx
Command Screenshot:


Result Screenshot:

Example 5: Download a list of files in library ‘Document’ of site ‘https://tenant.sharepoint.com/sites/Plan’ 

$filelist | Get-SPOFile -downloadPath "C:\documents" -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document
Command Screenshot:



Result Screenshot:

Example 6: Download a folder 2013 from the folder ‘PlanLib’ of library ‘Document’ of site ‘https://tenant.sharepoint.com/sites/Plan’ 

Get-SPOFolder -downloadPath "C:\documents" -userName user@tenant.onmicrosoft.com -siteURL "https://tenant.sharepoint.com/sites/Plan" -ListName Document -FolderName PlanLib/2013
Command Screenshot:

Result Screenshot:

 

 

Additional Resources

Technical Resources: 

Windows PowerShell Advanced Function
How to: Complete basic operations using SharePoint 2013 client library code