Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

10 thoughts on “SharePoint Online: Upload Files to Document Library using PowerShell

  • October 15, 2021 at 5:15 PM

    We have a powershell error on line 36 “$Context.ExecuteQuery() ” for uploading one file: any idea?

  • August 22, 2021 at 3:10 AM

    Thank you very much Rajack, I really appreciate it. I was already very tired of looking for information to be able to upload files through Programming Languages, “A full month”, “I did not succeed because of issues on my side and customer security issues.” I found your blog and without having knowledge of PowerShell, I managed to complement it with all the examples that you expose and I managed to make a script that allows me to upload files in bulk, in any folder and with only the url of the sharepoint site along with the access variables . Thanks very much buddy.

  • January 4, 2021 at 7:39 AM

    Hi is there anyway to get the details for the uploaded files in a log file ? like the last modified date and time, upload date, name of the respective file, and other things. If yes please guide me a little towards the same, as i am completely new to powershell and sharepoint, but after bulk uploading files successfully using this article,(it was really great,thanks for the article). But i suppose if we would have been able to keep track of all the files than it would be much more useful to the user.
    Any help is appreciated.
    Thanks 🙂

  • July 23, 2020 at 4:14 PM

    I had crazy problem with a library with spaces in the name. It was between quotes like “this is my library” but that gave me a crazy error message in powershell. >>> “Identity client Runtime Library (IDCRL) could not look up the real information for federated sign-in” If you google this message you get so many different hits but nothing worked. So i just tried to upload to the default “Documents” folder in Sharepoint Online and it worked perfectly. Then the Sharepoint Admin removed the spaces in the original library name and then that folder worked too. So…dont use spaces in Library names ! Thank you very much for the script, maybe you should write a hint in the script regarding that “no spaces” problem. This will save many hours in the future i think. Thanks again my friend !

    • July 23, 2020 at 6:46 PM

      That’s sounds Odd! I’m able to upload to libraries that contains space with in CSOM and PnP! Are you missing double quotes that wraps Library name by any chance?

  • May 22, 2020 at 12:35 AM

    Very useful information, thank you. Would like to add the logic to recreate the exact sub-folder structure in documents up to sharepointonline library. Think I need to make two passes, one to create all the sub-folders and sub-sub-folders, etc. and then one to copy the files in to all the proper folders. Don’t want them all in the root library folder. Need them in same folder locations as they are on the disk.

  • February 28, 2019 at 5:04 PM

    HI, Great script. QQ: How do I upload document to a folder under a library. Thanks

    • April 4, 2019 at 9:22 AM

      You can try this one 🙂
      #Load SharePoint CSOM Assemblies
      Add-Type -Path “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll”
      Add-Type -Path “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll”

      #Set parameter values for site

      #Set parameter values for Library

      #Set parameter values for Sub Directory under Documents Library

      #Set parameter values for Destination Directory

      #Set parameter values for File to download

      #Setup Credentials to connect
      $global:AdminName =”user”
      $global:AdminPassword =”password”

      Function Get-Context($SiteURL){

      #Setup Credentials to connect
      $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($global:AdminName,(ConvertTo-SecureString $global:AdminPassword -AsPlainText -Force))

      #Setup the context
      $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
      $Ctx.Credentials = $Credentials

      #Rturn the value ctx
      return $ctx


      # Fuction for uploading the file
      Function Uploading-FilesFromLibrary()
      #check parameter for SiteURL,LibraryName,TargetFolder,File and Subdir
      [Parameter(Mandatory=$true)] [string] $SiteURL,
      [Parameter(Mandatory=$true)] [string] $LibraryName,
      [Parameter(Mandatory=$true)] [string] $Sourcefile,
      [Parameter(Mandatory=$true)] [string] $upload_location

      Try {

      #Setup the context
      $ctx = Get-Context -SiteURL $SiteURL

      #Retrieve list
      $List = $ctx.Web.Lists.GetByTitle($LibraryName)

      $TargetFolder = $ctx.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + $upload_location)

      #Upload file
      Foreach ($File in (dir $Sourcefile -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 = $TargetFolder.Files.Add($FileCreationInfo)

      Write-host -f Green “‘$File’ from Library ‘$Sourcefile’ Uploaded to Folder ‘$upload_location’ Successfully!” $_.Exception.Message


      #Write-host -f Green “‘$File’ from Library ‘$Sourcefile’ Uploaded to Folder ‘$upload_location’ Successfully!” $_.Exception.Message

      Catch {
      write-host -f Red “Error Uploading Files from Library!” $_.Exception.Message

      #Call the function to Uploading file
      Uploading-FilesFromLibrary -SiteURL $SiteURL -LibraryName $LibraryName -Sourcefile $Sourcefile -upload_location $upload_location


Leave a Reply