Microsoft 365 – PowerShell script – Get all SharePoint sites where respective security group has permissions using SharePoint online PowerShell (connecting Tenant using UserName and Password)

Microsoft 365 - PowerShell script - Get all SharePoint sites where respective security group has permissions using SharePoint online PowerShell (connecting Tenant using UserName and Password)
Microsoft 365 - PowerShell script - Get all SharePoint sites where respective security group has permissions using SharePoint online PowerShell (connecting Tenant using UserName and Password)

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

Today one more PowerShell script for Microsoft 365

Details :

Detailed Steps :

  • Connect to M365 tenant using Connect-SPOService
#Connect to our M365 tenant - Please change here the tenant SharePoint site admin URL
Connect-SPOService "https://knowledgejunction-admin.sharepoint.com/"

  • Get all SharePoint sites from our Tenant using Get-SPOSite
$spoSites = Get-SPOSite -Limit All

  • We will loop through each site
  • We will fetch all users from the respective site using – Get-SPOUser
  • While querying we will filter based on our security group display name
  • Following is the code for same
#verifying the groups of every site
foreach ($spoSite in $spoSites)
{ 
    try{
    $groups = Get-SPOUser -Site $spoSite.Url -Limit All | Where { $_.IsGroup -and $_.DisplayName -ne 
            "Everyone" -and $_.DisplayName -ne "Everyone except external users" -and $_.DisplayName -like 
            '*SecurityGroupName*'} 

    # verifying each group    
    foreach ($group in $groups)
    {
            Write-Host $group.DisplayName $spoSite.Url -ForegroundColor "Cyan"   
           
            #preparing object to store result - which will be exported to CSV file
            $obj = New-Object Pscustomobject -Property @{
                     SiteURL = $spoSite.URL
                     GroupName = $group.Displayname
            }
            $Report += $obj
                
    }#foreach ($group in $groups)
        
   }catch{continue;}   
 }#foreach ($spoSite in $spoSites)

  • Finally export our object to CSV file
$Report | Export-Csv $Path -NoTypeInformation

Complete PowerShell script :

<#  
    Outputs a .csv file of records that represent list of SPO sites which contains the specific given group     
    from the tenant it is run in. 
    Result feature records will include:
        - SharePoint site URL
        - Groups which SharePoint site have
#>

#Path of CSV file where we need to generate 
$Path = 'c:\sitegroups.csv'

#Connect to our M365 tenant - Please change here the teanant SharePoint site admin URL
Connect-SPOService "https://knowledgejunction-admin.sharepoint.com/"

#Get all SharePoint sites from our Tenant
$spoSites = Get-SPOSite -Limit All

$report = @()

#verifying the groups of every site
foreach ($spoSite in $spoSites)
{ 
    try{
    $groups = Get-SPOUser -Site $spoSite.Url -Limit All | Where { $_.IsGroup -and $_.DisplayName -ne "Everyone" -and $_.DisplayName -ne "Everyone except external users" -and $_.DisplayName -like '*SecurityGroupName*'} 

    # verifying each group    
    foreach ($group in $groups)
    {
            Write-Host $group.DisplayName $spoSite.Url -ForegroundColor "Cyan"   
            #$spoSite.Url + "," + $group.DisplayName | Out-File -FilePath $Path -Append
            #$obj = new-object pscustomObject

            $obj = New-Object Pscustomobject -Property @{
            SiteURL = $spoSite.URL
            GroupName = $group.Displayname
            }
            $Report += $obj

            #break;
                
    }#foreach ($group in $groups)
        
   }catch{continue;}   
 }#foreach ($spoSite in $spoSites)

 $Report | Export-Csv $Path -NoTypeInformation

We have lots of PowerShell scripts for SharePoint On-Premises / SharePoint Online, please have a look – https://knowledge-junction.com/category/technology-articles/powershell-cmdlets/

In case if you want any PowerShell script, we will try to include in our PowerShell collection 🙂 SHARING IS CARING 🙂

Thanks for reading 🙂 STAY SAFE 🙂 STAY HEALTHY 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: