Have to search a particular group into all SPO sites and subsites and have to get the site URL and the group name where its added

Sunny Rastogi 106 Reputation points
2021-01-23T19:19:38.017+00:00

Hi Team,

I have requirement, I have to search a particular group name (Ex. Designer) into all active SPO sites, sub sites (Tenant Level) and have to extract the Site URL and the Group name where its added.

Please let me know if you have any PS script or CSOM code.

Looking for your feedback.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,920 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,623 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,967 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,574 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amos Wu-MSFT 4,051 Reputation points
    2021-01-25T03:21:49.4+00:00

    Hi @Sunny Rastogi ,
    We could use pnp powershell to achieve this.

    1. Get all site collections in the tenant and get the group in the root site.
    2. Get all subsites in the current site collection and get the group in the root site.

    Code for your reference:

    $username = "amos@contoso.onmicrosoft.com"  
    $password = "password"  
    $groupName = "Designers"  
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)  
    Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/dev -Credentials $cred  
    $sites=Get-PnPTenantSite  
      
    for($i=0; $i -le $sites.length; $i++){  
          
         
        try {   
            Connect-PnPOnline -Url $sites[$i].URL -Credentials $cred  
            $group=Get-PnPGroup -Identity $groupName -ErrorAction Stop  
             if($group -ne $null){  
                Write-Host $sites[$i].URL $group.Title -Foregroundcolor green           
             }  
                   
         }  
            catch {  
                   Write-Host $sites[$i].URL "$($_.Exception.Message)" -Foregroundcolor Red                         
                   }  
              $subwebs=Get-PnPSubWebs -Recurse   
              if($subwebs -ne $null)  {  
                for($j=0; $j -le $subwebs.length; $j++){  
                    
                    try {   
                    Connect-PnPOnline -Url $subwebs[$j].URL -Credentials $cred  
                    $group=Get-PnPGroup -Identity $groupName -ErrorAction Stop  
                    if($group -ne $null){  
                        Write-Host $subwebs[$j].URL $group.Title -Foregroundcolor green     
                    }  
                                 
                    }  
                    catch {  
                    Write-Host $subwebs[$j].URL "$($_.Exception.Message)" -Foregroundcolor Red                         
                   }  
                }  
              }  
                
        }  
    

    It will list the sites containing this group in green, and the sites without this group in red.

    59928-capturfffe.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.