SharePoint Online: Upload Files to Document Library using PowerShell

Requirement: PowerShell to upload file to SharePoint Online document library.
sharepoint online Upload Files to Document Library using PowerShell

How to Upload a File to SharePoint Online Document Library using PowerShell:
Let's upload a document using PowerShell CSOM in SharePoint Online. Here is how to upload files to SharePoint online.
#Load SharePoint CSOM Assemblies
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"
 
#Variables for Processing
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales/"
$LibraryName ="Documents"
$SourceFile="C:\SitesToCreate.csv"
$AdminName ="Salaudeen@crescent.com"
$AdminPassword ="password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
$Context.Credentials = $Credentials

#Get the Library
$Library =  $Context.Web.Lists.GetByTitle($LibraryName)

#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFile)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $SourceFile -leaf
  
#sharepoint online upload file powershell
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $SourceFileName
$FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
 
#Execute
$Context.Load($FileUploaded) 
$Context.ExecuteQuery() 

#Close file stream
$FileStream.Close()
 
write-host "File has been uploaded!" 
This PowerShell copies single document to SharePoint Online from local drive. If you need PowerShell to upload file to SharePoint Online document library sub-folder, refer: SharePoint Online: PowerShell to Upload File to Folder, Alright. How about bulk upload using PowerShell?

Upload All Files from a Folder to SharePoint online Library:
Now, lets upload all files from a local folder to SharePoint Online library using PowerShell.
#Load SharePoint CSOM Assemblies
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"
 
### Variables for Processing
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales/"
$LibraryName ="Documents"
$SourceFolder="C:\Documents"
$UserName ="Salaudeen@crescent.com"
$Password ="password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
$Context.Credentials = $Credentials

#Get the Library
$Library =  $Context.Web.Lists.GetByTitle($LibraryName)

#upload each file from the directory
Foreach ($File in  (dir $SourceFolder -File))
{
    #Get the file from disk
    $FileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()
  
    #Upload the File to SharePoint Library
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
 
    #Execute
    $Context.Load($FileUploaded) 
    $Context.ExecuteQuery() 

    #Close file stream
    $FileStream.Close()

    write-host "File: $($File) has been uploaded!"
} 
This uploads files to SharePoint Online using PowerShell. If you need to set the metadata of a file, use: PowerShell to Update metadata of a File in SharePoint Online Document Library
sharepoint online upload document powershell
If you need a PowerShell script to upload multiple files-folders to SharePoint Online: Upload multiple files to SharePoint Online using PowerShell

PnP PowerShell to Upload File to SharePoint Online Document Library
Here is the PnP PowerShell to Copy file to SharePoint Online document library from local drive.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$SourceFilePath ="C:\Documents\Discloser Asia.docx"
$DestinationPath = "/Shared Documents" #Relative Path of the Library
 
#Get Credentials to connect
$Cred = Get-Credential
 
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
     
#Upload File to SharePoint Online Library
Add-PnPFile -Path $SourceFilePath -Folder $DestinationPath

PnP PowerShell to Upload All Files from a Folder to SharePoint Online:
Let's use PnP PowerShell to bulk copy all files from local folder to SharePoint Online Library. Here is the PowerShell to upload file to SharePoint Online document library with metadata.
#Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$FilesPath = "C:\Upload"
$SiteRelativePath = "/Shared Documents/"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Files from a Local Folder
$Files = Get-ChildItem -Path $FilesPath -Force -Recurse

#Upload All files from the directory
ForEach ($File in $Files)
{
 Write-host "Uploading $($File.Directory)\$($File.Name)"
 
 #Upload File and Set Metadata
 Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder $SiteRelativePath -Values @{"Title" = $($File.Name)}
}
SharePoint Online: Upload Files to Document Library using PowerShell SharePoint Online: Upload Files to Document Library using PowerShell Reviewed by Salaudeen Rajack on June 04, 2016 Rating: 5

3 comments:

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

    ReplyDelete
    Replies
    1. You can try this one :)
      #Load SharePoint CSOM Assemblies
      Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
      Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.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

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.