Share via

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

Cheri Bell 26 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

}

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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