This SharePoint Online Automation tutorial, we will discuss how to add files to the SharePoint Online document library remotely using PowerShell in SharePoint Online Office 365.
In this article, we will learn about O365 SharePoint Online- How to Upload your files remotely using PowerShell to SPO document Library.
It’s a very common query and requirement with Admins to Upload the files which are placed in the local system to SharePoint sites to avoid manual tasks, Risks, and hassle-free process.
Upload Files Remotely to SharePoint Online Document Library using PowerShell
Open Windows PowerShell ISE with Admin rights and copy below script.

Here we need to update below details:
- $User = User Name of your Office 365 account
- $Password = Your Account password
- $Site URL: Site URL of Office 365 SharePoint Online where you want to copy or move a document
- $Folder: Destination location from here it will copy your files and copy to destination URL
$DocLibName: Document Library name where you want to Copy the Files which will take from a Local system.
#Specify tenant admin and site URL
$User=”Your id”
$Password=’Your Password’
$SiteURL=”Site URL/”
$Folder=”C:\Script\test”#Path where you want to Copy
$DocLibName=”Documents”#Docs library
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type-Path”C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type-Path”C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
#Bind to site collection
$Context=New-ObjectMicrosoft.SharePoint.Client.ClientContext($SiteURL)
$Creds=New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($User,(ConvertTo-SecureString$Password-AsPlainText-Force))
$Context.Credentials=$Creds
#Retrieve list
$List=$Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
#Upload file
Foreach ($Filein (dir$Folder-File))
{
$FileStream=New-ObjectIO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo=New-ObjectMicrosoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite=$true
$FileCreationInfo.ContentStream=$FileStream
$FileCreationInfo.URL =$File
$Upload=$List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
Save the above script once all changes have been done, In above script, you can see I have added Add-Type -path, which will refer reference file of SharePoint Online Management Shell.
We need to follow below steps to execute above command without any error:
- Ensure that SharePoint Client components SDK installed on the user machine if it’s not installed kindly download the same executable file and install to your local system:
- SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll paths mentioned in the script are same as user’s machine
- Set the “execution policies” for PowerShell in user machine by executing Remote signed
Set-ExecutionPolicyRemotesigned
Once you have verified all pre-requisites, you are ready to run the script which will upload your files from local system to SharePoint site URL under a document library. Now click on F5 and wait for some time to get upload all files which are copied from local system to SharePoint Online Document Library.
You may like following SharePoint PowerShell tutorials:
- Getting Started with PnP PowerShell – SharePoint 2013/2016/SharePoint Online
- Bulk SharePoint Online Site Collection Creation using PowerShell
- PowerShell Script to deploy WSP Solution in SharePoint 2013
- Working with PowerShell in SharePoint Online/2016/2013
- Working with PowerShell Variables
- SharePoint 2016 PowerShell Script to list all Users in Site Collection
- Create a web application in SharePoint 2016 from Central Administration and using PowerShell
- SharePoint 2013 backup and restore using PowerShell
- Retrieve List Items Created in Last N days using PowerShell SharePoint Online
- Delete list items created before N days using PowerShell in SharePoint Online
- the list is too large to save as a template the size of a template cannot exceed 524288000 bytes
- Get SharePoint document library size using PowerShell
I hope this SharePoint Online tutorial explains, how to upload files to the SharePoint document library using PowerShell remotely in SharePoint Online Office 365.
I am Bijay a Microsoft MVP (8 times – My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com