Upload document using PowerShell Remotely in SharePoint Online

upload file to sharepoint online powershell

In my previous articles we learned how to upload documents in SharePoint server with both Invoke-WebRequest and System.Net.WebClient approaches to upload documents to an on premise SharePoint sites. Here we will discuss how to upload document using PowerShell Remotely in SharePoint Online.

In this article I would like to speak about the process to upload the documents to SharePoint Online site from your local environment. However the requirement is still remain same as last time that user want to migrate all the documents from his network / local drive to SharePoint online document library as part data migration. Since there are millions of files in user’s drive, I can’t ask the user to upload all his files one by one nor with the explorer view. Copying files manually will take quite a good time of time.

Read SharePoint 2013 tutorials

Solution:
The below PowerShell script uploads all the user documents to SharePoint Online document library. This script I can run request end user to execute on his local machine as and when he / she require. Another way is that I can run on behalf of business user on my machine seamlessly.



The power shell script is as follows:

try {
#Specify tenant admin and site URL and user name
$User = “krishna@OnlySharePoint2013.onmicrosoft.com”
$SiteURL = https://onlysharepoint2013.sharepoint.com/sites/krishna
#Specify Local folder name where the files are to be uploaded
$Folder = “G:\KVN Articles\PS Testing\Files”
$DocLibName = “Finance”

$startedTime = Get-Date
Write-Host “Time Started = ” $startedTime

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path “C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll”
#Reading Password from PowerShell Command line
$Password = Read-Host -Prompt “Please enter your password” -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Creating Object to Document Library
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Reading through the local folder for files
Foreach ($File in (dir $Folder -File))
{
#Reading File data using IO stream object
$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
#Uploading file to document library
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
Write-Host “Uploaded File=” $File.Name
}
$completedTime = Get-Date
Write-Host “Time Completed = ” $completedTime
Write-Host “Script Excecuted Successfully !!!”
}
catch {
write-host “Error: $($_.Exception.Message)” -foregroundcolor red
}

I strongly believe that the script is self-explanatory as I have added comments for most of the lines.

Note:
To execute this script user must install the SharePoint client components SDK on his/her machine. SDK can be downloaded from – http://www.microsoft.com/en-us/download/details.aspx?id=35585

Steps to be followed to ensure the smooth execution:



1. Ensure that SharePoint Client components SDK installed on user machine
2. Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll paths mentioned in the script are same as user’s machine
3. Open PowerShell command-let with Run as Administrator
4. Check the “execution policies” for PowerShell in user machine by executing below command in PowerShell command let
Get-ExecutionPolicy -List
5. The above command will get all the existing policies of the user mostly the output will be as follows:

Scope ExecutionPolicy
MachinePolicy Undefined
UserPolicy            Undefined
Process Undefined
CurrentUser Undefined
LocalMachine            Undefined

6. We need to change the execution policy from Undefined to RemotelySigned with the below command
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
7. Ensure that the policy got updated properly by executing the Get command as part of step-4
8. Now execute the PowerShell script as “.\<<ScriptFilename.ps1>> by navigating on the respective folder location of the script
9. Once the files are uploaded successfully, please revert the execution policies back to its original state by executing the below line
Set-ExecutionPolicy Undefined -Scope LocalMachine

Output:
Files uploaded to SharePoint Library:
In the output you can see the uploaded user name.

upload file to sharepoint online powershell
upload file to sharepoint online powershell

Hope this is helpful !!!

Similar SharePoint 2013 Tutorials


Leave a Reply