Home > database >  SharePoint OnLine - PowerShell PNP - List folders that have attribute
SharePoint OnLine - PowerShell PNP - List folders that have attribute

Time:10-29

$siteURL="https://URL"
$folder="/FOLDER"

Function GetFolders($folderUrl)  
{      
    $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder   
    # Loop through the folders  
    foreach($folder in $folderColl)  
    {                      
       write-host -ForegroundColor Green $folder.Name  
    }           
}  
  
# Connect to SharePoint Online site  
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
  
# Call the functions  
GetFolders($folder)

I'm trying to list folders that contains string from tab below. Example, string is "important_documents" enter image description here

I have managed to list all, but missing powershell knowledge to complete script :)

CodePudding user response:

My Document Library:

enter image description here

Please run the below PowerShell script as an admin:

#Variables
$SiteURL = "https://xxxx.sharepoint.com/sites/sitename"
$LibraryName = "{libraryname}"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
   
    #Get the Library
    $Library = $Ctx.web.Lists.GetByTitle($LibraryName)
    $Folders = $Library.RootFolder.Folders
    $Ctx.Load($Folders)
    $Ctx.executeQuery()
 
    #Get the Folder by Name
    $Folder = $Folders | Where {$_.Name -like "*important_documents*"} 
    foreach($Fol in $Folder){
    #Get Some Folder Properties
        Write-host "Folder Name:"$Fol.Name
        Write-host -f Green "Total Number of Files in the Folder:"$Fol.ItemCount
        Write-host ""
        }   
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

enter image description here

CodePudding user response:

$siteURL="https://URL"
$folder="/FOLDER"

Function GetFolders($folderUrl)  
{      
    $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder
    # Loop through the folders  
    foreach($folder in $folderColl)  
    {                      
        $newFolderURL= $folderUrl "/" $folder.Name
        GetFolders($newFolderURL)
        $a = $newFolderURL
        
        if($a -like "*important_documents*"){
        write-host -ForegroundColor Green $a
        Get-PnPList | Where {$_.Hidden -eq $false}
        }
    }
}  
  
# Connect to SharePoint Online site  
Connect-PnPOnline –Url $siteURL –Credentials (Get-Credential)  
  
# Call the functions  
GetFolders($folder)  

Update: I have managed to list all folders and subfolders that contains important_documents. In mu case, it's working slow, cause I have a lot of subfolders.

  • Related