Microsoft 365 / SharePoint online : PowerShell script – to get the list of SharePoint sites where given group has permission and export sites to CSV file

PowerShell script - Verify given group permission across Tenant
PowerShell script - Verify given group permission across Tenant

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

Today something new – PowerShell script 🙂

Background : My management want to check on which SharePoint sites if one particular group has permissions. So this script 🙂

Details :

  • High level steps as follows:
    • Connect to our SharePoint tenant
    • Get all sites from the Tenant
    • For every site get all Groups
    • For every group check if the group which we want to verify is available in group list which is fetched from above step
    • If above condition is true then write the given SharePoint site URL to CSV file

Detailed Steps – PowerShell script :

  • Connect to our SharePoint tenant using – Connect-SPOService cmdlet

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

  • Fetch all sites from our tenant using – Get-SPOSite -Limit All

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

  • Loop through each site and get all the groups of site

#verifying the groups of every site
foreach ($spoSite in $spoSites)
{
    Write-Host $spoSite.Url -ForegroundColor "Yellow"
    
    #fetching all the groups from the site
    $groups = Get-SPOSiteGroup -Site $spoSite.Url
    

  • Next, we will loop through each group and check if respective site contains the group which we need to verify
  • If above condition is TRUE then write the SharePoint site URL to CSV file. Here we are using – Out-File cmdlet

 # verifying each group    
    foreach ($group in $groups)
    {
        #check for our specific group
        if($group.Title.Contains("TeamsDemo_16062020.11.28 Members"))
        {
            Write-Host $group.Title -ForegroundColor "Cyan"   

            # writing the SharePoint site URL to CSV file
            $spoSite.Url | Out-File -FilePath $Path -Append
            break;
        }         
                
    }#foreach ($group in $groups)
           
 }#foreach ($spoSite in $spoSites)

PowerShell script - Verify given group permission across Tenant
fig : PowerShell script – Verify given group permission across Tenant

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

#>

#CSV file path
$Path = 'c:\sitegroups.csv'

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

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

#preparing array list
#$spoSitesListings = [System.Collections.ArrayList]@()

#verifying the groups of every site
foreach ($spoSite in $spoSites)
{
    Write-Host $spoSite.Url -ForegroundColor "Yellow"
    
    #fetching all the groups from the site
    $groups = Get-SPOSiteGroup -Site $spoSite.Url
    
    
    # verifying each group    
    foreach ($group in $groups)
    {
        #check for our specific group
        if($group.Title.Contains("TeamsDemo_16062020.11.28 Members"))
        {
            Write-Host $group.Title -ForegroundColor "Cyan"   

            $spoSite.Url | Out-File -FilePath $Path -Append
            break;
        }         
                
    }#foreach ($group in $groups)
           
 }#foreach ($spoSite in $spoSites)

Knowledge Junction has very handy / useful set of PowerShell scripts , well tested – Please have a look list once – https://knowledge-junction.com/category/technology-articles/powershell-cmdlets/

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...

2 Responses

  1. October 1, 2021

    […] Recently we have published article for PowerShell script to find the permission of SharePoint group on all site collection in respective tenant – Microsoft 365 / SharePoint online : PowerShell script – to get the list of SharePoint sites where … […]

  2. October 4, 2021

    […] PowerShell script to find the permission of SharePoint group on all site collection in respective tenant – Microsoft 365 / SharePoint online : PowerShell script – to get the list of SharePoint sites where … […]

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

%d bloggers like this: