In Microsoft 365, managing groups is a crucial aspect of maintaining an organized and secure environment. In this article, we will walk you through a Graph PowerShell script that lists the count of security groups and distribution groups within your tenant. We will also explain how the script works, suggest enhancements, discuss possible errors and solutions, and conclude with the importance of such scripts in your daily administrative tasks.
Here’s the script that retrieves the count of security groups and distribution groups:
Connect-MgGraph -Scopes "Group.Read.All"
$allGroups = Get-MgGroup -All -Property DisplayName, MailEnabled, SecurityEnabled, GroupTypes
$m365Groups = $allGroups | Where-Object {
$_.GroupTypes -contains "Unified"
}
$distributionGroups = $allGroups | Where-Object {
($_.MailEnabled -eq $true) -and
($_.SecurityEnabled -eq $false) -and
(-not ($_.GroupTypes -contains "Unified"))
}
Write-Output "Microsoft 365 Groups Count: $($m365Groups.Count)"
Write-Output "Distribution Groups Count: $($distributionGroups.Count)"
Disconnect-MgGraph
Script Output:
Connect-MgGraph
cmdlet with the required scope Group.Read.All
.Get-MgGroup
cmdlet. The -All
parameter ensures that all groups are fetched and the -Property GroupTypes
parameter ensures that the GroupTypes property is included in the results.Disconnect-MgGraph
cmdlet.Here are a few ways to enhance this script:
Example enhancement to export results:
# Export the group counts to a CSV file
$groupCounts = [PSCustomObject]@{
SecurityGroupsCount = $securityGroupCount
DistributionGroupsCount = $distributionGroupCount
}
$groupCounts | Export-Csv -Path "GroupCounts.csv" -NoTypeInformation
Error: Unauthorized Access
Message:
Connect-MgGraph : Insufficient privileges to complete the operation.
Solution: Ensure that the account you are using has the Group.Read.All
permission. You may need to grant the necessary permissions in Azure Active Directory and re-authenticate.
Error: Network Issues
Message:
Connect-MgGraph : A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond.
Solution: Check your internet connection and ensure that your network allows connections to Microsoft Graph.
Error: Throttling
Message:
Get-MgGroup : Too many requests.
Solution: Implement retry logic with exponential backoff to handle throttling by Microsoft Graph.
Example of retry logic:
$retryCount = 0
$maxRetries = 5
$retryDelay = 2
do {
try {
$allGroups = Get-MgGroup -All -Property GroupTypes
$success = $true
} catch {
$retryCount++
Start-Sleep -Seconds $retryDelay
$retryDelay *= 2
}
} until ($success -or $retryCount -ge $maxRetries)
if (-not $success) {
Write-Error "Failed to retrieve groups after multiple attempts."
exit
}
groupTypes/any(c:c eq 'Unified')
to Count Microsoft 365 Groups"Unified"
value in the groupTypes
property.mailEnabled eq true
and Exclude Unified to Count Distribution Groups"Unified"
in their groupTypes
.mailEnabled eq true
and exclude groupTypes/any(c:c eq 'Unified')
to separate them from Microsoft 365 groups.
Using Graph PowerShell to manage and report on groups within your Microsoft 365 tenant is a powerful and efficient approach. The script provided in this article helps you quickly ascertain the number of security groups and distribution groups, allowing for better organization and security management. By understanding how to enhance the script and handle potential errors, you can tailor the solution to fit your specific needs and ensure robust group management in your environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex