Can i get direct folders that are under a parent folder using Get-PnPFolderItem

john john 986 Reputation points
2023-07-12T12:07:30.43+00:00

I have a SharePoint online document library which contain the following:-

Site url = /sites/Clients/

Document library = Customer Share

Main Folder name = Clients

now i want to get all the Direct folders that are under the Clients main folder, is this possible.

I have this PnP Power shell, but this will list everything inside the document library

$mainfolderUrl = "/sites/Clients/Customer Share/Clients"
$listName = "Customer Share"



Connect-PnPOnline -Url $SiteURL -Interactive
##$customerFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $mainfolderUrl -PageSize 1  -ScriptBlock {}

$customerFolders= Get-PnPListItem -List $listName -FolderServerRelativeUrl $mainfolderUrl -PageSize 1  -ScriptBlock {} | Select-Object id,@{label="Filename";expression={$_.FieldValues.FileLeafRef}},@{label="Fileurl";expression={$_.FieldValues.FileRef}}



ForEach($customerFolder in $customerFolders)
{
  $customerFolder
}

This is the result of the script:-

Id Filename                          Fileurl
-- --------                          -------
 4 ClientA                           /sites/Clients/Customer Share/Clientss/ClientA
 5 ClientB                           /sites/Clients/Customer Share/Clientss/ClientB
 6 ClientC                           /sites/Clients/Customer Share/Clientss/ClientC
13 underB                            /sites/Clients/Customer Share/Clientss/ClientB/underB
15 underb2                           /sites/Clients/Customer Share/Clientss/ClientB/underB/underb2
19 em                                /sites/Clients/Customer Share/Clientss/ClientC/em
 7 Questions-Database Programmer V1.docx /sites/Clients/Customer Share/Clientss/ClientA/Questio…
 8 Sharepoint-metabata-sitesV3.docx  /sites/Clients/Customer Share/Clientss/ClientA/Sharepoint-meta…
 9 Sharepoint-metabata-sites.docx    /sites/Clients/Customer Share/Clientss/ClientA/Sharepoint-meta…
10 Questions-Database Programmer.docx/sites/Clients/Customer Share/Clientss/ClientA/Questio…
11 1212 - Copy.xlsx                  /sites/Clients/Customer Share/Clientss/ClientB/1212 - Copy.xlsx
12 1212 - Copy - Copy.xlsx           /sites/Clients/Customer Share/Clientss/ClientB/1212 - Copy - C…
14 guru_card_export.csv              /sites/Clients/Customer Share/Clientss/ClientB/underB/guru_car…
16 SharePoint-quote.xlsx             /sites/Clients/Customer Share/Clientss/ClientB/underB/underb2/…
17 Pic-4.png                         /sites/Clients/Customer Share/Clientss/ClientC/Pic-4.png
18 Pic-3.png                         /sites/Clients/Customer Share/Clientss/ClientC/Pic-3.png
20 paras script.txt                   /sites/Clients/Customer Share/Clientss/ClientB/paras script.txt
21 guru_card_export (1).csv          /sites/Clients/Customer Share/Clientss/ClientB/underB/underb2/…
22 contracts.png                     /sites/Clients/Customer Share/Clientss/ClientB/underB/contract…
23 General IO.xlsx                  /sites/Clients/Customer Share/Clientss/ClientB/General…

now after getting the direct folders under the "Clients" main folder, i want to loop through the content of each of the sub-folders separatly .. can anyone advice? any advice?

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,267 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 28,796 Reputation points Microsoft Vendor
    2023-07-13T08:33:44.23+00:00

    Hi john john ,

    You can use the following PowerShell script to get all files in a subfolder.

    #Config Variables
    $SiteURL = "https://tenant.sharepoint.com/sites/amy12345"
    $FolderSiteRelativeUrl = "/Shared Documents/Division 2/Customer 1" #Folder's Site Relative Path
      
    #Get Credentials to connect
    $Cred = Get-Credential
      
    Try {
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteURL -Credentials $Cred
      
        #Get All Files from the Folder
        $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl -Recursive
         
        Write-host "Total Number of Files in the Folder:" $FolderItems.Count
        ForEach($File in $FolderItems)
        {
            $File.Name
        }
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
    

    The result:

    07131

    07132

    07133


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.