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 :
- From one of our team I got request to find on which site collections in our tenant have permission to specific security group
- Recently we have published two article related to this
- 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 given group has permission and export sites to CSV file
- Microsoft 365 – PowerShell script – Get all SharePoint sites where respective security group has permissions – using PnP PowerShell (connecting Tenant using Azure app client id and client secret key) – https://knowledge-junction.in/2021/10/01/microsoft-365-powershell-script-get-all-sharepoint-sites-where-respective-security-group-has-permissions-using-pnp-powershell-connecting-tenant-using-azure-app-client-id-and-client-secret-key/
- In this article we will discuss PowerShell script step by step to know the permission of Security group on all site collection from the tenant using user name and password
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 🙂
1 Response
[…] This content was originally published here. […]