How to Get the Folder and all Sub-Folders GUIDs using Power Shell

Cheri Bell 21 Reputation points
2024-05-01T17:09:13.0666667+00:00

I have a request to provide a list of sub-Folder GUIDs. I can generate one Folder GUID from this script, but I need the sub-folders within it.

Can you assist with a script that will provide a list of sub-folder GUIDs?

Cheri

#Variables

$SiteURL = "https://company.sharepoint.com/sites/Benefit"

$ServerRelativeUrl= "/Folder/Folder/Folder/Accounts"

Try {

#Setup the context

Connect-PnPOnline -Url https://company.sharepoint.com/sites/Benefit -UseWebLogin

$Ctx =  Get-PnPContext

#Get the web from URL

$Web = $Ctx.web

$Ctx.Load($Web)

$Ctx.executeQuery()

#Get the Folder object by Server Relative URL

$Folder = Get-PnPFolder -Url $ServerRelativeUrl -Includes ListItemAllFields

$folderGuid = $Folder.ListItemAllFields["GUID"]

$Ctx.Load($Folder)

$Ctx.ExecuteQuery() 

#Get Some Folder Properties

Write-host -f Green "Folder GUID:"$Folder.ListItemAllFields["GUID"] 

}

Catch {

write-host -f Red "Error Getting Folder!" $_.Exception.Message

}

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,946 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,676 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 36,651 Reputation points Microsoft Vendor
    2024-05-02T01:35:26.3433333+00:00

    Hi @Cheri Bell,

    You could refer to following script to get all subfolders

    #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 Variables
    $SiteURL = "https://xxx.sharepoint.com/sites/name"
    $ListName = "Documents"
     
    #Get Credentials to connect
    $Cred = Get-Credential
      
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
     
        #Get the List
        $List = $Ctx.web.Lists.GetByTitle($ListName)
         
        #Define CAML Query to get all folders from list recursively
        $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
        $CAMLQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>1</Value></Eq></Where></Query></View>"
     
        #Get All Folders from the List
        $Folders = $List.GetItems($CAMLQuery)
        $Ctx.Load($Folders)
        $Ctx.ExecuteQuery()
     
        #Iterate through Each Folder
        ForEach($Folder in $Folders)
        {
            #Get the Folder's Server Relative URL
            Write-host $Folder.FieldValues['FileRef']
    		
        }
    }
    catch {
        write-host "Error: $($_.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.


0 additional answers

Sort by: Most helpful

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.