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
$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
    $TargetFolder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)

    #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)  

    #Close file stream
    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

To upload a file to a folder in SharePoint Online document library using PnP PowerShell, use:

#Config Variables
$SiteURL = ""
$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 these parameters: NewFileName, Checkout, Approve, Publish, ContentType, Values. If you need to upload all files and folders from a local drive to SharePoint Online using PowerShell, use: SharePoint Online: Upload All Files & Sub-Folders using PowerShell

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!

3 thoughts on “SharePoint Online: Upload File to Sub-Folder using PowerShell

  • July 15, 2021 at 7:50 PM

    How do you modify this to upload all files, sub folders to a sub folder in sharepoint? Ex… How to upload c:\source\*.* to sharepoint sub folder at /sites/myteam/shared documents/general/subfolder

  • May 18, 2020 at 11:28 AM

    #i have created new script to upload files in subfolder in sharepoint

    #Specify tenant admin and site URL
    $User = “your user_name”
    $SiteURL = “https://*****”

    #local pc folder
    $Folder = “C:dest”

    $DocLibName = “Documents”

    #folder of sharepoint where you want to copy the file or folder
    $FolderName = “Data_Center/Backups/HRMS/”

    #install sharepoint sdk on your machine and give path of this in below code
    #Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
    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”

    #your password
    $Password = “********” | ConvertTo-SecureString -AsPlainText -Force

    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
    $Context.Credentials = $Creds

    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)

    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)

    $TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + “/” + $FolderName);

    #Upload file(s)
    Foreach ($File in (dir $Folder -File))
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
    $FileCreationInfo.URL = $File.Name
    $UploadFile = $TargetFolder.Files.Add($FileCreationInfo)


Leave a Reply