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.in/category/technology-articles/powershell-cmdlets/

Thanks for reading 🙂

STAY SAFE 🙂 STAY HEALTHY 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

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 … […]

Leave a Reply

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

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading