Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

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

  • 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 🙂

    Reply
  • 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 !

    Reply
    • 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?

      Reply
  • 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.

    Reply
  • February 28, 2019 at 5:04 PM

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

    Reply
    • 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
      $SiteURL=”https://crescent.sharepoint.com/Sites/Sales/”

      #Set parameter values for Library
      $LibraryName=”Documents”

      #Set parameter values for Sub Directory under Documents Library
      $upload_location=”/Folder2/Folder1/”

      #Set parameter values for Destination Directory
      $Sourcefile=”C:download”

      #Set parameter values for File to download
      #$File=”testcsv2.csv”

      #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
      param
      (
      [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)
      $ctx.Load($List.RootFolder)
      $ctx.ExecuteQuery()

      $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)
      $ctx.Load($TargetFolder)
      $ctx.ExecuteQuery()

      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

      Reply

Leave a Reply