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
- #Specify tenant admin and site URL
- $User = "Your id"
- $Password = 'Your Password'
- $SiteURL = "Site URL/"
- $Folder = "C:\Script\HpeQuota"
- #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 - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
- $Creds = New - Object Microsoft.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($File in (dir $Folder - File))
- {
- $FileStream = New - Object IO.FileStream($File.FullName, [System.IO.FileMode]::Open)
- $FileCreationInfo = New - Object Microsoft.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 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,
https://www.microsoft.com/en-us/download/details.aspx?id=35585
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.