SharePoint Online Automation - O365 - Upload Your Files Remotely Using PowerShell To SPO Document Library

Continuing with SharePoint Online Automation, today, I am going to demonstrate  “O365 SharePoint Online- How to Upload your files remotely using PowerShell to SPO document Library”.

In my last below articles, I have described you

In this article, we will learn about O365 SharePoint Online- How to Upload your files remotely using PowerShell to SPO document Library. It’s 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.

Let us start a live example step by step with screenshots,

  • Open PowerShell ISE with Admin rights and copy below script

As mentioned in last articles, we always need to do changes in the defined variable as per your requirement. Here, we need to update the following 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 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 Local system
    1. #Specify tenant admin and site URL  
    2. $User = "Your id"  
    3. $Password = 'Your Password'  
    4. $SiteURL = "Site URL/"  
    5. $Folder = "C:\Script\HpeQuota"  
    6. #Path where you want to Copy  
    7. $DocLibName = "Documents"  
    8. #Docs library  
    9. # Add references to SharePoint client assemblies and authenticate to Office 365 site - required  
    10. for CSOM  
    11. Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    12. Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"#  
    13. Bind to site collection  
    14. $Context = New - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
    15. $Creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo - SecureString $Password - AsPlainText - Force))  
    16. $Context.Credentials = $Creds  
    17. Retrieve list  
    18. $List = $Context.Web.Lists.GetByTitle($DocLibName)  
    19. $Context.Load($List)  
    20. $Context.ExecuteQuery()  
    21. # Upload file  
    22. Foreach($File in (dir $Folder - File))  
    23. {  
    24.     $FileStream = New - Object IO.FileStream($File.FullName, [System.IO.FileMode]::Open)  
    25.     $FileCreationInfo = New - Object Microsoft.SharePoint.Client.FileCreationInformation  
    26.     $FileCreationInfo.Overwrite = $true  
    27.     $FileCreationInfo.ContentStream = $FileStream  
    28.     $FileCreationInfo.URL = $File  
    29.     $Upload = $List.RootFolder.Files.Add($FileCreationInfo)  
    30.     $Context.Load($Upload)  
    31.     $Context.ExecuteQuery()  
    32. }  

Save the above script once all changes have been done. In the 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 the below steps to execute the above command without any error.

  • Ensure that SharePoint Client components SDK installed on user machine if it’s not installed kindly download the same executable file and install to your local system,
  • Microsoft.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-ExecutionPolicy Remotesigned

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 all files uploaded which are copied from local system to SharePoint Online Document Library.