Office 365 SharePoint Online Upload Files With Metadata Using PowerShell

This blog will help you to upload a file into SharePoint Online, using PowerShell method.
Steps
  1. Open SharePoint Management Shell.
  2. Copy the code given below and paste it.
  3. Run the code.
  1. ##########################    
  2.   
  3. #Script  : Below Script will upload file with metadata  in Office 365 SharePoint Online   
  4. #Author  : Gowtham Rajamanickam    
  5. #Date    :16-03-2017  
  6. #Version:1.0    
  7.   
  8. #########################  
  9.  
  10. #Before Run the Scritp you must load the Client Assembly files   
  11.    
  12. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  13. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  14.    
  15. $UserName= "[email protected]"  
  16. $Password = "Enter your Password here"  
  17. $SiteUrl =https://gowthamr.sharepoint.com  
  18.     
  19. $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))  
  20. $Context = New-Object Microsoft.SharePoint.Client.ClientContext()  
  21. $Context.credentials = $credentials  
  22. $filePath = "C:\Documents\GowthamsTutorialGuide.docx"  
  23. $listTitle = "MigratedDocuments"  
  24. $targetList = $Context.Web.Lists.GetByTitle($listTitle) #target list  
  25.   
  26. $fci= New-Object Microsoft.SharePoint.Client.FileCreationInformation  
  27. $fci.Overwrite = $true  
  28. $fci.Content = [System.IO.File]::ReadAllBytes($filePath)  
  29. $fci.URL = [System.IO.Path]::GetFileName($filePath)  
  30. $uploadFile = $targetList.RootFolder.Files.Add($fci)  
  31.  
  32. #Set metadata properties  
  33. $listItem = $uploadFile.ListItemAllFields  
  34. $listItem["LastReviewed"] = [System.DateTime]::Now  
  35. $listItem.Update()  
  36.   
  37. $Context.Load($uploadFile)  
  38. $Context.ExecuteQuery()     
Was my blog helpful? Was any relevant content missing? If so, please let us know what's confusing or missing at the bottom of this page.

Thanks for reading my blog.
Ebook Download
View all
Learn
View all