SharePoint Online: Upload File to Sub-Folder using PowerShell

Requirement: SharePoint Online PowerShell to Upload File to Folder

PowerShell to Upload File to SharePoint Online Sub-Folder
Here is the PowerShell to upload file to SharePoint Online Document library subfolder.
#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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SourceFilePath="C:\Users\salaudeen\Desktop\Project Documents\Discloser Asia.doc"
$TargetFolderRelativeURL ="/sites/Marketing/Shared Documents/2018/Active"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
      
    #Get the Target Folder to upload
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $TargetFolder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)
    $Ctx.Load($TargetFolder)
    $Ctx.ExecuteQuery() 

    #Get the source file from disk
    $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
    #Get File Name from source file path
    $SourceFileName = Split-path $SourceFilePath -leaf   
    $TargetFileURL = $TargetFolderRelativeURL+"/"+$SourceFileName

    #Upload the File to SharePoint Library Folder
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $TargetFileURL
    $FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)  
    $Ctx.ExecuteQuery()  

    #Close file stream
    $FileStream.Close()
    Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
}
catch {
    write-host "Error Uploading File to Folder: $($_.Exception.Message)" -foregroundcolor Red
}
This PowerShell uploads file to SharePoint Online document library sub-folder.
upload file to sharepoint online folder powershell

Upload File to SharePoint Online Folder using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$SourceFilePath ="C:\Documents\SRS.Docx"
$DestinationFolderPath = "/Shared Documents/2018"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Upload File to Folder
    Add-PnPFile -Path $SourceFilePath -Folder $DestinationFolderPath -ErrorAction Stop
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
The Add-PnPFile cmdlet also supports parameters: NewFileName, Checkout, Approve, Publish, ContentType, Values.

If you need to upload all files & folders from a local drive to SharePoint Online using PowerShell, use: SharePoint Online: Upload All Files & Sub-Folders using PowerShell
SharePoint Online: Upload File to Sub-Folder using PowerShell SharePoint Online: Upload File to Sub-Folder using PowerShell Reviewed by Salaudeen Rajack on January 21, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.