How to add the list item in the folder on SharePoint online by PowerShell

WonChul Choi 0 Reputation points

Hello I try to add list items inside folder on SharePoint Online using by PowerShell script.

I use SharePoint.Client package because I updating the script

Anyway, I success to get list items and get sub folders in the List. However, I cannot add list items under sub folders. I keep failing to add inside list folder.

Could you please help me to add list item inside Sub folders ?

Script : RootFolder.Folders

$boxesList = $Context.web.Getlist($boxesListUrl)
    $allSubFolders = $boxesList.RootFolder.Folders

    $subFolder = $allSubFolders | where { $_.Name -eq $deptCode }
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
8,550 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
1,540 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 26,791 Reputation points Microsoft Vendor

    Hi @WonChul Choi,

    If you want to add a file to subfolder by powershell, you could refer to following script

    #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
    $TargetFolderRelativeURL ="/sites/testsite/Shared Documents/sufolder/folder"
    #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

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. WonChul Choi 0 Reputation points


    In fact, I know how to upload files under folder. However, my team created folders in the list to make list items categorize.

    Basically, when click the folder on list, sub list will be pop up on sharepoint online.

    Is there any way to add list items under sub list or sub folder?