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

1 Response

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