Fetch the Count of Public and Private Microsoft 365 Groups

Managing groups within a Microsoft 365 tenant can be a complex task, especially when you need to distinguish between public and private groups. With the power of Microsoft Graph and PowerShell, you can efficiently fetch the count of these groups. In this article, we’ll provide a script to get the count of public and private groups, explain the script, discuss its use cases, address possible errors, and conclude with the benefits of using this approach.


Script to Fetch the Count of Public & Private Groups

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All"

# Get all groups
$groups = Get-MgGroup -All -Property Visibility

# Count public groups
$publicGroupsCount = ($groups | Where-Object { $_.Visibility -eq "Public" }).Count

# Count private groups
$privateGroupsCount = ($groups | Where-Object { $_.Visibility -eq "Private" }).Count

# Output the counts
Write-Output "Total Public Groups: $publicGroupsCount"
Write-Output "Total Private Groups: $privateGroupsCount"

# Disconnect from Microsoft Graph
Disconnect-MgGraph


Script Explanation

Connect to Microsoft Graph:

Connect-MgGraph -Scopes "Group.Read.All"

This line connects to Microsoft Graph with the necessary permission to read all group information.

Get all groups:

$groups = Get-MgGroup -All -Property Visibility

This retrieves all groups in the tenant, specifically fetching their visibility property.

Count public groups:

$publicGroupsCount = ($groups | Where-Object { $_.Visibility -eq "Public" }).Count

This filters the groups to find those with a visibility of "Public" and counts them.

Count private groups:

$privateGroupsCount = ($groups | Where-Object { $_.Visibility -eq "Private" }).Count

Similar to the public groups count, this line filters and counts the groups with a visibility of "Private".

Output the counts:

Write-Output "Total Public Groups: $publicGroupsCount"
Write-Output "Total Private Groups: $privateGroupsCount"

This prints the counts of public and private groups to the console.

Disconnect from Microsoft Graph:

Disconnect-MgGraph

This disconnects the session from Microsoft Graph.


Use Cases

  • Inventory Management: IT administrators can use this script to maintain an up-to-date inventory of group types in the organization.
  • Security Audits: During security audits, knowing the number of public vs. private groups can help assess exposure and access control.
  • Reporting: Generate regular reports on group visibility status to ensure compliance with organizational policies.

Possible Errors and Solutions

Authentication Errors:

Error: Connect-MgGraph : The provided application ID does not exist.

Solution: Ensure the correct application ID and permissions are used. You may need to register an application in Azure AD and grant it the necessary permissions.

Insufficient Permissions:

Error: Get-MgGroup : Insufficient privileges to complete the operation.

Solution: Verify that the account used to run the script has the Group.Read.All permission. You might need to consent to this permission when prompted.

Network Issues:

Error: Connect-MgGraph : A connection attempt failed because the connected party did not properly respond.

Solution: Check your internet connection and ensure there are no firewalls or network issues blocking the connection to Microsoft Graph.

Unexpected Script Termination:

Error: Get-MgGroup : An unexpected error occurred.

Solution: Add error handling to your script to capture and log errors for further investigation. Use Try-Catch blocks in PowerShell to handle exceptions.


Conclusion

Using Microsoft Graph and PowerShell to fetch the count of public and private groups in your Microsoft 365 tenant simplifies group management. This approach is efficient, scalable, and provides clear insights into the structure of your tenant's groups. By integrating this script into your regular administrative tasks, you can enhance visibility and control over your organization's group settings, ultimately contributing to better security and compliance.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex